0

我想匹配输入字段中给出的字符串。

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.

我正在初始化一个 boost 正则表达式解析器,如下所示:

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

regex_match 不应该匹配,但不幸的是它匹配......

我检查了几个在线正则表达式测试器:我的正则表达式不匹配(如预期的那样)。

知道这可能是提升的错误还是我做错了什么?

Debuggex Demo:点我验证("[\"']((:?[^\"']|\\")+?)[\"']"

谢谢

4

2 回答 2

1

试试下面的表达式:

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

正则表达式 101 演示

于 2013-11-07T14:36:13.780 回答
0

对于这个简单的检查,正则表达式是多余的。只需检查字符串是否有左引号,然后搜索下一个不带反斜杠的引号。如果第二个引号不在末尾,则字符串的格式不正确。

于 2013-11-07T14:35:21.207 回答