好的,所以我正在尝试学习c ++正则表达式,但遇到了一些麻烦。我仔细阅读了代码,至少对我而言,这是合乎逻辑的。我还使用正则表达式测试仪在线测试了它,并成功匹配了我的字符串。前两个(nameParser
和anotherNameParser
)不起作用,但最后一个(sampleParser
)起作用。我真的不明白为什么它不能验证我的字符串。下面,我包括一个屏幕截图:
//http://rextester.com/tester
//compile with g++ -std=gnu++11 main.cpp -o main
#include <iostream>
#include <regex>
using namespace std;
/* * (nameParser)
* Beginning at the front of the string, any set of character followed by at least one or more spaces
* followed by any set of characters with exactly one preceding dot,
* then followed by at least one or more spaces followed by any set of characters and end of string
*/
//Need the extended or basic because any version less than 4.9 doesn't fully support c++11 regular expressions (28.13).
//The error is because creating a regex by default uses ECMAScript syntax for the expression, which doesn't support brackets.
const regex nameParser("^[a-zA-Z]+\\s+[a-zA-Z]\\.{1}\\s+[a-zA-Z]+$",regex::extended);
const regex anotherNameParser("[a-zA-Z]+",regex::extended);
const regex sampleParser("(abc)");
int main() {
//simple regex testing
string name = "bob R. Santiago";
if(regex_match(name, nameParser)) {
cout << "name is valid!" << endl;
} else cout << "Error in valdiating name!" << endl;
string anotherName = "Bobo";
if(regex_match(anotherName, anotherNameParser)) {
cout << "name is valid!" << endl;
} else cout << "Error in valdiating name!" << endl;
string sample = "abc";
if(regex_match(sample, sampleParser)) {
cout << "name is valid!" << endl;
} else cout << "Error in valdiating name!" << endl;
return 0;
}
//Need the extended or basic because gnu gcc-4.6.1 doesn't fully support c++11 regular expressions (28.13).
它不支持他们的所有,直到4.9版本,尽管存在<regex>
头。
有时,它似乎可以执行您想要的操作,但实际上并没有。
升级到GCC 4.9。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句