首页 > 解决方案 > 是否期望 Boost 正则表达式空白字符类也匹配垂直制表符

问题描述

我注意到字符类[:blank:]也匹配\v,如下面的代码所示。但是,根据 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;
}

显然,由于Boost 的这个页面没有提到我在这里看到的所有字符类,我怀疑 Boost 正则表达式不符合 POSIX,即使它们使用了其中一些命名的字符类。好吧,在那个 Boost 页面上甚至没有POSIX这个词,所以我想我几乎是在回答自己,但我觉得不够自信。

我还没有检查这些字符中的哪个属于[:blank:]and/or [: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";

标签: c++regexboostposixline-breaks

解决方案


更新:
有关控制 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行为,语法选项类型是扩展的。

有关 POSIX 扩展选项信息,请参阅本节:

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

_____________________-

我不知道这是否会改变[[:blank:]]匹配的内容,此时
我无法使用导入提升库创建测试 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

推荐阅读