繁体   English   中英

提高特殊字符的正则表达式模式

[英]boost regex pattern for special characters

有谁知道如何在c ++中为特殊字符制作boost模式?

这不仅意味着("[A-Za-z0-9 \\\\s]*")

但是_ + - \\ ) \\\\ ( . | ]等,例如这样的字符串:

"hello world \\\\has (special.characters) + defined_with[boost]"有效

"!hello world \\\\has (special.characters) + defined_with[boost]"无效

更具体地说,是这样的:

string input;

getline(cin,input);

boost::regex pattern1 ("[a-zA-Z0-9 \\s \\_ \\+ \\- \\\ \\) \\\\ \\( \\. \\| ]*");

if (!regex_match (input, pattern1))
    {
    cout << "invalid input" << endl;
    }
    else
        cout << input << " - is valid" << endl;

我会很感激您能提供的任何帮助

到目前为止,我还没有使用Boost:regex,但是我对Regex有所了解。 我假设您仅在存在至少一个“特殊字符”的情况下才匹配。 此外,我假设您只希望字母数字字母有效,而其余字母无效。 就像“好字符串”和“ \\ Bad.String)”一样。 请尝试对此进行更具体的说明。

据我所知,首先只匹配一个零或多个“ *”的类将始终返回true。 仅当您至少有一个类实例时,才尝试使用“ +”进行匹配。

其次,您可以尝试使用类似[^ a-zA-Z0-9]的类,如果类中没有其他内容,则结果为true。

为了进一步阅读,我建议使用文档(是的,我知道,正则表达式的语法真是地狱): http : //www.boost.org/doc/libs/1_53_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax .html或我最喜欢的正则表达式语法http://en.wikipedia.org/wiki/Regular_expression

我希望这有帮助!

我建议做一个排除不合格字符的集合,例如:

[^\\\\!\\\\?]+ ,然后测试是否匹配

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM