正则表达式匹配多个否定先行 [英] Regex matching multiple negative lookahead

查看:106
本文介绍了正则表达式匹配多个否定先行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅在字符串不以"abc:"或"defg:"开头的情况下,我才尝试匹配一个字符串(使用Perl正则表达式),但是我似乎找不到答案.我已经尝试过类似的

I'm trying to match a string (using a Perl regex) only if it doesn't start with "abc:" or "defg:", but I can't seem to find out how. I've tried something like

^(?:(?!abc:)|(?!defg:))

推荐答案

先行者(?=foo)(?!foo)和后方(?<=foo)(?<!foo)不使用任何字符.

Lookahead (?=foo), (?!foo) and lookbehind (?<=foo), (?<!foo) do not consume any characters.

您可以执行多个断言:

^(?!abc:)(?!defg:)

或:

^(?!defg:)(?!abc:)

...而且顺序没有什么不同.

...and the order does not make a difference.

这篇关于正则表达式匹配多个否定先行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆