整数匹配正则表达式模式不工作 [英] integer-matching regex pattern not working

查看:120
本文介绍了整数匹配正则表达式模式不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

我尝试检查每当输入字符串是整数时, #include< iostream>
#include< string>
#include< regex>
using namespace std;

int main()
{
string input =1;
string int_num =^ 0 $ | ^ [1-9] [0-9] * $;

regex pattern(int_num,regex_constants :: grep);

if(regex_match(input,pattern))
cout<< matched \\\
;
else
cout<< not matching\\\
;

return 0;
}


解决方案

首先,regex模式应该改为^(0 | [1-9] [0-9] *)$,因为它看起来更好。然后,将 regex_constants :: grep 更改为 regex_constants :: ECMAScript ,或留空,因为ECMAScript是默认值。最后,需要使用 g ++ - 4.9 编译,因为g ++ - 4.8版本不能与regex lib一起使用。


I try to check whenever the input string is an integer, but nothing returns positive result with the pattern I created.

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

int main()
{
    string input = "1";
    string int_num = "^0$|^[1-9][0-9]*$";

    regex pattern(int_num, regex_constants::grep);

    if(regex_match(input, pattern))
        cout << "matched\n";
    else
        cout << "not matched\n";

    return 0;
}

解决方案

First of all, regex pattern should be changed to "^(0|[1-9][0-9]*)$" because it looks better. Then, change regex_constants::grep to regex_constants::ECMAScript, or leave blank, because ECMAScript is default. At the end, it is required to compile with g++-4.9, because g++-4.8 version doesn't work with regex lib.

这篇关于整数匹配正则表达式模式不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆