加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 正则表达否定整个单词?

发布时间:2020-12-15 05:11:43 所属栏目:Java 来源:网络整理
导读:参见英文答案 Regular Expressions and negating a whole character group ????????????????????????????????????9个 String test1 = "This is my test string"; 我想匹配一个不包含“test”的字符串 我可以做到 Pattern p = Pattern.compile(".*?^(test).*?
参见英文答案 > Regular Expressions and negating a whole character group ????????????????????????????????????9个

String test1 = "This is my test string";

我想匹配一个不包含“test”的字符串

我可以做到

Pattern p = Pattern.compile(".*?^(test).*?")

它可以工作,但在大多数网站,如Regular Expressions and negating a whole character group
????^(?!.* 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]匹配除以下任何字符:(t,e,s,t)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读