【发布时间】:2021-04-16 15:18:42
【问题描述】:
我有一个 regex101 可以正常工作的正则表达式:
按照预期,有 2 个匹配项。
现在我想用 std 的 regex_token_iterator 拆分相同的内容:
const std::string text = "This is a test string [more or less] and here is [another].";
const std::regex ws_re("\(?<=\[)(.*?)(?=\])\gm"); // HOW TO WRITE THE ABOVE REGEX IN HERE?
std::copy( std::sregex_token_iterator(text.begin(), text.end(), ws_re, -1),
std::sregex_token_iterator(),
std::ostream_iterator<std::string>(std::cout, "\n"));
这编译得很好,但没有任何东西打印到标准输出。
我认为正则表达式必须以其他方式编写,您能指出我的错误吗?
【问题讨论】:
-
\ 必须转义或使用原始字符串文字。
-
尝试使用
\\(?<=\\[)(.*?)(?=\\])\\gm。仍然没有打印任何内容。 -
或者使用原始字符串。
-
你不应该在正则表达式中有
\gm。 -
为什么第一个括号被转义了,而其他的不是?