为什么我的C ++正则表达式不起作用?

达米安

好的,所以我正在尝试学习c ++正则表达式,但遇到了一些麻烦。我仔细阅读了代码,至少对我而言,这是合乎逻辑的。我还使用正则表达式测试仪在线测试了它,并成功匹配了我的字符串。前两个(nameParseranotherNameParser)不起作用,但最后一个(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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么此正则表达式在Notepad ++(Windows)中不起作用?

我的正则表达式代码不起作用

为什么我更换正则表达式的时间不起作用?

为什么与项目标签匹配的正则表达式不起作用?

为什么我的pcregrep正则表达式中的正向提前不起作用?

为什么延迟匹配在此正则表达式中不起作用?

为什么我的正则表达式在PHP中不起作用?

为什么我的正则表达式中的可选捕获组不起作用?

为什么我的grep +正则表达式不起作用?

为什么我的grep +正则表达式不起作用?

为什么我的带连字符的正则表达式不起作用?

为什么我的正则表达式在X中起作用但在Y中不起作用?

我简单的正则表达式不起作用

我的正则表达式不起作用

为什么我的正则表达式在string.match()内不起作用?

为什么此正则表达式在grep中不起作用?

为什么正则表达式在SQL Server Select语句中不起作用?

为什么我的正则表达式不起作用?

发现:为什么带有egrep的正则表达式不起作用?

为什么我的正则表达式在Java中不起作用

为什么我的正则表达式组量词不起作用?

为什么我的范围正则表达式语法不起作用?

为什么带有 \[ 的正则表达式不起作用

无法理解为什么我的 Java 正则表达式不起作用

即使正则表达式可以,为什么用正则表达式替换也不起作用?

为什么我的正则表达式电子邮件验证不起作用?

为什么我的正则表达式不起作用?[Java] [正则表达式] [空白问题]

为什么标记行首的正则表达式不起作用?

为什么我在函数中的正则表达式在 PHP 中不起作用?