2 回答
TA貢獻1777條經驗 獲得超10個贊
您可以嘗試以下正則表達式:
^(this.*?)(test)?$
正則表達式的解釋:
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
this 'this'
--------------------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
( group and capture to \2 (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
test 'test'
--------------------------------------------------------------------------------
)? end of \2 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \2)
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
TA貢獻1817條經驗 獲得超6個贊
你可以試試這位朋友
^(?:(?!test))(?:(.*)(?=\btest\b)(\btest\b)|(.*))
解釋
^(?:(?!test))- 負面展望。不要匹配任何以測試開頭的內容。(.*)- 匹配除換行符以外的任何內容。(?=\btest\b)- 積極的前瞻。test單詞邊界之間的匹配。(\btest\b)- 捕獲組比賽test。|- 交替與邏輯 OR 相同。(.*)- 匹配除換行符以外的任何內容。
添加回答
舉報
