2
我目前在Ubuntu下使用gcc-4.9在C++ 11库中进行实验。很明显,我不明白正则表达式的(默认)ECMAScript语法。C++ 11问题regex_search和ECMAScript语法
我的代码如下:
#include <iostream>
#include <exception>
#include <regex>
using namespace std;
int main() {
string test_str = "this is text containing teeext! Text! Teeeeeeeeeext! This txt should not be matched, nor this Txt";
string pattern = "([Tt][e]+xt)";
try {
regex r(pattern, regex_constants::ECMAScript);
smatch results;
if (regex_search(test_str, results, r))
{
cout << "Found #" << results.size() << " results!" << endl;
for(unsigned int i = 0; i < results.size(); i++)
{
cout << results[i].str() << endl;
}
}
else
cout << "no match for " << pattern << endl;
} catch (regex_error &e) {
cout << "what: " << e.what() << "; code: " << e.code() << endl;
}
}
我期望的正则表达式“文本”,“teeext”,“文本”和“Teeeeeeeeeext”原始字符串匹配里面;但输出是:
Found #2 results!
text
text
我已经尝试过多种方式指定正则表达式,如"[T|t]e+xt"
和变化,但似乎没有任何工作。我究竟做错了什么?
谢谢你的时间。
国旗是一个错误,我只是删除它^ _ ^; – Alberto 2015-02-06 14:49:35
可能的重复[如何使用新的C++ 0x regex对象在字符串中重复匹配?](http://stackoverflow.com/questions/5586733/how-do-i-use-the-new- C0X正则表达式对象到匹配反复中之串) – Andreas 2015-02-06 14:54:54