首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

容易正则匹配不成功

2013-07-16 
简单正则匹配不成功#include iostream#include string#include regexusing namespace stdint main(

简单正则匹配不成功

#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
//

热点排行