【发布时间】:2013-04-13 11:27:37
【问题描述】:
我在使用标准正则表达式时遇到问题。我无法使问号量词起作用。对 regex_match 的调用将始终返回 0。
我也尝试过使用 {0,1},它的行为也不像我预期的那样:它的行为就像一个 + 量词。
这是我的代码:
#include <iostream>
#include <regex>
using namespace std;
int main(int argc, char **argv){
regex e1("ab?c");
cout << regex_match("ac", e1) << endl; // expected : 1, output 0
cout << regex_match("abc", e1) << endl; // expected : 1, output 0
cout << regex_match("abbc", e1) << endl; // expected : 0, output 0
regex e2("ab{0,1}c");
cout << regex_match("ac", e2) << endl; // expected : 1, output 0
cout << regex_match("abc", e2) << endl; // expected : 1, output 1
cout << regex_match("abbc", e2) << endl; // expected : 0, output 1
return 0;
}
我使用以下命令编译:
g++ -std=c++11 main.cpp -o regex_test
我在这里做错了吗?或者为什么它不起作用?
【问题讨论】:
-
据我所知,
std::regex没有正确/完全实施。 (但我不知道是这里的情况还是其他情况。) -
我尝试了 20 多种变体,转义
?、括号、使用*和{x,y}都以某种方式失败。我认为<regex>有问题。 -
是的,我听说过类似的事情,实际上可能就是这样。我已经切换到提升正则表达式(这有效),但我正在等待更多答案