假设我有以下字符串:Stackoverflow 可以帮助您
。
我的正则表达式应该是这样的:is
和 here
之间不应包含 not
。可以存在其他子串。 not
可以出现在字符串中的任何其他位置。
例如:
Stackoverflow 不是来帮助您的
- 不应该匹配
Stackoverflow 随时为您提供帮助
- 应该匹配
Stackoverflow 始终为您提供帮助
- 应该匹配
Stackoverflow 并不总是为您提供帮助
- 不应该匹配
最佳答案
这个正则表达式可以工作:
^.* is((?!not).)* here .*$
这是一个示例:regex101
^: asserts position at start of a line
.* matches any character
" is": matches the signal word "is" (with blanks to match only "is")
((?!not).)*: matches all words which are not "not"
" here ": matches the signal word "here" (with blanks to match only "here")
.* matches any character
$ asserts position at the end of a line
更新
^.* is((?! not )( .*))* here .*$
使用该正则表达式,您可以指定该正则表达式仅适用于“is”和“here”。
( .*): blank for a new word
更新的示例:regex101
关于Java正则表达式用于具有两个子字符串且其间不包含特定子字符串的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60389158/