我在使用 std 正则表达式时遇到了麻烦。我无法使问号量词起作用。对 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
我在这里做错了什么吗?或者为什么它不起作用?
你的正则表达式代码没问题。您正在使用的实现不是。它提供了一个标头,声明了库实现得很糟糕的一堆东西。如果一个商业软件包做到了这一点,它将受到严厉的批评,这是理所当然的。你得到你所付出的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)