读书人

容易正则匹配不成功

发布时间: 2013-07-16 22:38:05 作者: rapoo

简单正则匹配不成功

#include <iostream>
#include <string>
#include <regex>
using namespace std;

int main() {
string str = ".method public native testIntFromCarson()I";
regex pattern("met*od");
smatch m;

if(regex_search(str, m, pattern)) {
cout << "match" << endl;
} else {
cout << "not match" << endl;
}
}

str包含 method 字符串, pattern搜索的是method,应该能搜到啊,输出为什么是 not match

在线运行:http://ideone.com/z9uVGC
[解决办法]
"met*od"
匹配如下字符串:
"meod" "metod" "mettod" "metttod"……
但不匹配
"method"

你的意思可能是:
"met.*od"

[解决办法]
#include <iostream>
#include <string>
#include <regex>
using namespace std;

int main() {
string str = ".method public native testIntFromCarson()I";
regex pattern(string("met.*od"));
smatch m;

if(regex_search(str, m, pattern)) {
cout << "match" << endl;
} else {
cout << "not match" << endl;
}
return 0;
}
//match
//

读书人网 >C++

热点推荐