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 相同。(.*)
- 匹配除换行符以外的任何内容。
添加回答
举报