[英]preg_match a programming string
我想要的是一个匹配php字符串的正则表达式。 所以这意味着"
和"
之间的所有内容,不包括\\"
。”到目前为止,我的表达已经存在
/"([^\\"]*)"/As
但那个是不正确的。 例如,使用此字符串
"te\\"st"
它匹配"tes\\
虽然它应匹配"te\\"st"
。 我也试过这个:
/"([^\\"].*)"/As
它正确匹配上面的字符串,但当我有这个字符串"te\\"st""
它匹配"te\\"st""
,而它应该只匹配"te\\"st"
任何帮助赞赏!
这是你想要的正则表达式:
(?<!\\)"(?:\\\\|\\"|[^"\r\n])*+"
我可以让你变短,但它不会那么坚固。
看演示 。
说明
(?<!\\\\)
断言前面的内容不是反斜杠 (?:\\\\\\\\|\\\\"|[^"\\r\\n])
匹配双反斜杠,转义引号\\"
或任何非引号和换行符的字符(假设一行上有字符串;否则取出\\r\\n
) *
重复零次或多次+
防止回溯 "
匹配收盘价 要使用它:
$regex = '/(?<!\\\\)"(?:\\\\\\\\|\\\\"|[^"\r\n])*+"/';
if (preg_match($regex, $yourstring, $m)) {
$thematch = $m[0];
}
else { // no match...
}
您可以使用此模式:
/\A"(?>[^"\\]+|\\.)*"/s
在PHP代码中你必须写:
$pattern = '/\A"(?>[^"\\\]+|\\\.)*"/s';
图案细节:
\A # anchor for the start of the string
"
(?> # open an atomic group (a group once closed can't give back characters)
[^"\\]+ # all characters except quotes and backslashes one or more times
|
\\. # an escaped character
)*
"
/s singleline mode (the dot can match newlines too)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.