Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试验证字符串是否对应于带有 stl 的正则表达式,如下所示:
regex rgx("#^\([ \r\t\n\f]*([-]?[0-9]+)[ \r\t\n\f]*,[ \r\t\n\f]*([-]?[0-9]+)[ \r\t\n\f]*\)$#"); bool test = regex_search("(12,3)", rgx);
该字符串应该匹配,但之后 test = false !
您需要将文字包装为原始文字:
regex rgx(R"#(^\([ \r\t\n\f]*([-]?[0-9]+)[ \r\t\n\f]*,[ \r\t\n\f]*([-]?[0-9]+)[ \r\t\n\f]*\)$)#");
顺便说一句,我相信以下正则表达式与您的相同,但更简单:
regex rgx(R"#(^\(\s*(-?\d+)\s*,\s*(-?\d+)\s*\)$)#");
或者没有原始文字:
regex rgx("^\\(\\s*(-?\\d+)\\s*,\\s*(-?\\d+)\\s*\\)$")