[英]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.