简体   繁体   中英

boost::regex_match gives different result than many online reggex tester

I want to match a string given in an input field.

A sample data could be "hello" -> returns true 
or "\"" -> returns true 
or "this is a string" -> returns true 
but """ should not be recognized as a string and should return false when checked by the regexp.

I am initializing a boost regex parser as follow:

    std::string myString = "\"\"\"";
    boost::smatch match;
    boost::regex regExpString3("[\"']((:?[^\"']|\\\")+?)[\"']");
    bool statusString3 = boost::regex_match(myString, match, regExpString3);

The regex_match should not match but unfortunately it does match ...

I checked on several online reggex tester: my regular expression did not match (as expected).

Any idea if this could be a bug of boost or am I doing something wrong ?

Debuggex Demo: Click me to verify ("[\\"']((:?[^\\"']|\\\\")+?)[\\"']"

Thanks

Try the following expression:

([\\"'])(?:[^\\"]|\\\\")+\\1

Regex101 Demo

A regular expression is overkill for this simple check. Just check the string for an opening quotation mark, then search for the next quotation mark that isn't precede by a backslash. If that second quotation mark isn't at the end, the string isn't in the correct format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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