【问题标题】:Is it expected that Boost regex blank character class also matches vertical tab是否期望 Boost 正则表达式空白字符类也匹配垂直制表符
【发布时间】:2021-01-07 22:20:33
【问题描述】:

我注意到字符类[:blank:] 也匹配\v,如下面的代码所示。不过,那个shouldn't be there, per POSIX,应该吗?

#include <string>
#include <iostream>
#include <boost/regex.hpp>
using namespace std;
using namespace boost;
int main() {
    std::string const text{"\v"};
    cout << (sregex_token_iterator{text.begin(), text.end(), regex{R"((?-m)^([[:blank:]])$)"}} != sregex_token_iterator{});
    cout << (sregex_token_iterator{text.begin(), text.end(), regex{R"((?-m)^([ \t])$)"}} != sregex_token_iterator{}) << '\n';
    // output: 10, but I expected 00
    return 0;
}

显然,由于this page of Boost 没有提及我看到列出的here 的所有字符类,我怀疑Boost 正则表达式不符合POSIX,即使它们使用一些那些命名的字符类。嗯,连 POSIX 这个词都没有出现在那个 Boost 页面上,所以我想我几乎是在回答自己,但我觉得不够自信。

我还没有检查过这些字符中的哪一个属于[:blank:] 和/或[:space:],但我猜这里可能还会有其他惊喜:

const auto LF   = "\x0A";
const auto VT   = "\x0B";
const auto FF   = "\x0C";
const auto CR   = "\x0D";
const auto CRLF = "\x0D\x0A";
const auto NEL  = "\xC2\x85";
const auto LS   = "\xE2\x80\xA8";
const auto PS   = "\xE2\x80\xA9";

【问题讨论】:

  • 但是,根据 POSIX,它不应该存在吗?:不,它不应该存在。 [不相关]:标准正则表达式库中没有这样的错误。 Example。坚持使用提升标题的任何具体理由?
  • @brc-dd,其实有这么一个原因,here。
  • 是的,在 Boost 中,他们扩展了对 unicode 字符串的支持。对于std::regex,只能直接使用std::string和std::wstring。当然,如果您正在处理 unicode 字符串,标准库将无法工作。但对于 ASCII 码,这是我推荐的选项。我期待 C++23 会提供一些支持。

标签: c++ regex boost posix line-breaks


【解决方案1】:

更新:
有关控制 Boost Regex 引擎工作方式的具体方法的信息。

引擎的行为可以根据
标志选项集。

见:http://boost.sourceforge.net/libs/regex/doc/syntax_option_type.html

概要摘录:

Type syntax_option type is an implementation specific bitmask type that controls how a regular expression string is to be interpreted.  For convenience note that all the constants listed here, are also duplicated within the scope of class template basic_regex.

namespace std{ namespace regex_constants{

typedef implementation-specific-bitmask-type syntax_option_type;

// these flags are standardized:
static const syntax_option_type normal;
static const syntax_option_type ECMAScript = normal;
static const syntax_option_type JavaScript = normal;
static const syntax_option_type JScript = normal;
static const syntax_option_type perl = normal;
static const syntax_option_type basic;
static const syntax_option_type sed = basic;
static const syntax_option_type extended;
static const syntax_option_type awk;
static const syntax_option_type grep;
static const syntax_option_type egrep;
static const syntax_option_type icase;
static const syntax_option_type nosubs;
static const syntax_option_type optimize;
static const syntax_option_type collate;
// other boost.regex specific options are listed below

} // namespace regex_constants
} // namespace std

看来语法类型也应该改变引擎匹配的行为。
对于特定的 POSIX 行为,语法选项类型为 extended。

有关 POSIX 扩展选项信息,请参阅此部分:

http://boost.sourceforge.net/libs/regex/doc/syntax_option_type.html#extended

_____________________-

我不知道这是否会改变 [[:blank:]] 匹配的内容
我无法使用 import boost 库创建测试 C++ 程序
此时。

如果有人尝试过,请告诉我为该类找到了什么。 -谢谢


原创
这只是我的测试,我只能使用 Perl 选项
使用我截至此日期的设置。

看起来像 [[:blank:]] 匹配 18 个 Unicode (utf-8) 代码点

00 0009    <control-0009>
00 0020    SPACE
00 00A0    NO-BREAK SPACE
00 1680    OGHAM SPACE MARK
00 2000    EN QUAD
00 2001    EM QUAD
00 2002    EN SPACE
00 2003    EM SPACE
00 2004    THREE-PER-EM SPACE
00 2005    FOUR-PER-EM SPACE
00 2006    SIX-PER-EM SPACE
00 2007    FIGURE SPACE
00 2008    PUNCTUATION SPACE
00 2009    THIN SPACE
00 200A    HAIR SPACE
00 202F    NARROW NO-BREAK SPACE
00 205F    MEDIUM MATHEMATICAL SPACE
00 3000    IDEOGRAPHIC SPACE

还有 4 个 (utf-16) 代码点

00 0009    <control-0009>
00 0020    SPACE
00 00A0    NO-BREAK SPACE
00 3000    IDEOGRAPHIC SPACE

【讨论】:

  • 这似乎强化了 Boost 的 [:blank:] 存在漏洞,因为它应该只匹配空格和(水平)制表符。
  • 哦,绝对的。另一方面,您可以为 POSIX 设置一些标志,这些标志可能会给您带来更好的 Boost 结果。
  • 你能从你的答案中链接一些东西吗?
  • 我认为你必须设置扩展标志,不确定。见boost.sourceforge.net/libs/regex/doc/…。真的,我上面使用的过滤器是针对 perl 而不是 POSIX。我可能应该删除它。
  • 所以像 static const syntax_option_type extended; 一样使用 extended rx("", extended);只是猜测
猜你喜欢
  • 2018-05-03
  • 2010-12-21
  • 2015-07-27
  • 1970-01-01
  • 2017-05-09
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多