簡體   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