String test1 = "This is my test string";
我想匹配一个不包含“test”的字符串
我可以用
Pattern p = Pattern.compile(".*?^(test).*?")
它有效,但在大多数网站上,例如正则表达式和否定整个字符组
^(?!.*test).*$
建议这对我不起作用。
据我的理解^(test)
就足够了,为什么^(?!.*test).*$
是必须的?
您想要以下内容。
^(?:(?!test).)*$
正则表达式:
^ the beginning of the string
(?: group, but do not capture (0 or more times)
(?! look ahead to see if there is not:
test 'test'
) end of look-ahead
. any character except \n
)* end of grouping
$ before an optional \n, and the end of the string
与使用^(test)
,它只是寻找test在字符串的开头,而不是否定它。
否定^
运算符只能在字符类内部工作[^ ]
,但整个单词在字符类中不起作用。例如[^test]
匹配任何字符,除了:(t
, e
, s
, t
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)