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

正则表达式 – 匹配字符串不包含一个组,DOES包含另一个组

发布时间:2020-12-14 06:04:25 所属栏目:百科 来源:网络整理
导读:例如,匹配不包含“foo”但包含“bar”的字符串的正则表达式.我没有使用两个表达式的奢侈.我需要在一个表达式中完成它. 例: “this is foo baz bar” – should not match “this is foo baz” – should not match “this is baz bar” – should match 我
例如,匹配不包含“foo”但包含“bar”的字符串的正则表达式.我没有使用两个表达式的奢侈.我需要在一个表达式中完成它.

例:

“this is foo baz bar” – should not match
“this is foo baz” – should
not match
“this is baz bar” – should match

我在Perl,fwiw

解决方法

在开始时使用 negative lookahead断言来检查字符串是否包含任何foo子字符串.

^(?!.*?foo).*bar.*

要么

^(?!.*?bfoob).*bbarb.*

DEMO

(编辑:李大同)

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

    推荐文章
      热点阅读