我想匹配输入字段中给出的字符串。
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.
我正在初始化一个提升正则表达式解析器,如下所示:
std::string myString = """"";
boost::smatch match;
boost::regex regExpString3("["']((:?[^"']|\")+?)["']");
bool statusString3 = boost::regex_match(myString, match, regExpString3);
regex_match不应该匹配,但不幸的是它确实匹配......
我检查了几个在线雷鬼测试器:我的正则表达式不匹配(如预期的那样)。
知道这可能是一个提升的错误还是我做错了什么?
调试演示:单击我进行验证("[\"']((:?[^\"']|\\")+?)[\"']"
谢谢
尝试以下表达式:
([\"'])(?:[^\"]|\\")+\1
正则表达式 101 演示
正则表达式对于这个简单的检查来说是矫枉过正的。只需检查字符串中的左引号,然后搜索前面没有反斜杠的下一个引号。如果第二个引号不在末尾,则字符串的格式不正确。