Java正则表达式用于具有两个子字符串且其间不包含特定子字符串的模式

标签 java regex

假设我有以下字符串:Stackoverflow 可以帮助您

我的正则表达式应该是这样的:ishere 之间不应包含 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/

相关文章:

java - 对象数组赋值问题

java - Hibernate OneToMany 关联表字段对于repository.save(entityModel) 不能为空

javascript - 如何根据 JSON 属性名称替换字符串值

MySQL 我可以使用正则表达式来表示行的类型吗?

java.lang.IllegalArgumentException : Context must not be null

java - 使用PowerMock模拟SecurityUtils中Shiro的静态方法

java - 带有 Heroku 的 Firebase Admin SDK

regex - 正则语言的定义

java - java中整数的正则表达式

java - 使用高级正则表达式分割字符串