【发布时间】:2015-09-28 11:28:04
【问题描述】:
出于某种原因,我必须在 Ubuntu 14.04 g++(版本 4.8.4)中使用 POSIX 的 regex.h
但是,似乎正则表达式在这种环境下不起作用
#include <iostream>
#include <regex.h>
using namespace std;
int main()
{
regex_t regex;
int result = regcomp(®ex, "RESERVE [A-Z][a-z]*[A-Z][a-z]* [0-9]{7}", 0);
if(result){
cout<<"ERROR"<<endl;
}
result = regexec(®ex, "RESERVE MrKim 1111111", 0, NULL, 0);
if(!result){
cout<<"MATCH"<<endl;
}
else if(result == REG_NOMATCH){
cout<<"NO MATCH"<<endl;
}
else{
char msg[100];
regerror(result, ®ex, msg, 100);
cout<<msg<<endl;
}
}
上面的程序只打印“NO MATCH”...
请给我一个建议。
【问题讨论】:
-
使用
REG_EXTENDED。 -
不,我怀疑它是重复的,它不是 C++,而是 C。
-
这是cpp代码,但由于兼容性,我不能使用c++11标准库
<regex>。 -
@stribizhev 谢谢。效果很好。
-
@stribizhev:将其发布为答案,或许可以描述它解决问题的原因。