我正在嘗試創建一個正則運算式來匹配不包括[first].
# Trying to match:
# [second]
# [first.second]
# [first.third]
[first]
# something = else
[second]
test = yes
[first.second]
[first.third]
我在嘗試 ^\[((?!first).*)\]$
https://regex101.com/r/1fz1CW/1
這似乎與[second]上面的示例匹配,但我不知道為什么它不匹配,[first.second]或者[first.third]我認為我可能需要單詞邊界,但我似乎無法讓它們作業。
uj5u.com熱心網友回復:
利用
^\[((?!first\])[^\]\[]*)\]$
請參閱正則運算式證明。
解釋
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
\[ '['
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
first 'first'
--------------------------------------------------------------------------------
\] ']'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
[^\]\[]* any character except: '\]', '\[' (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\] ']'
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/408174.html
標籤:
