如何否定整个正则表达式? [英] How to negate the whole regex?

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

问题描述

我有一个正则表达式,例如 (ma|(t){1}).它匹配 mat 并且不匹配 bla.

I have a regex, for example (ma|(t){1}). It matches ma and t and doesn't match bla.

我想否定正则表达式,因此它必须匹配 bla 而不是 mat为此添加一些东西正则表达式.我知道我可以写 bla,但实际的正则表达式更复杂.

I want to negate the regex, thus it must match bla and not ma and t, by adding something to this regex. I know I can write bla, the actual regex is however more complex.

推荐答案

Use negative lookaround: (?!pattern)

Use negative lookaround: (?!pattern)

积极环顾可用于断言模式匹配.负环顾相反:它用于断言模式不匹配.有些风格支持断言;有些对后视等进行了限制.

Positive lookarounds can be used to assert that a pattern matches. Negative lookarounds is the opposite: it's used to assert that a pattern DOES NOT match. Some flavor supports assertions; some puts limitations on lookbehind, etc.

这些尝试为玩具问题提出正则表达式解决方案作为练习;如果您正在尝试学习使用环视的各种方式(嵌套它们、使用它们进行捕获等),它们应该具有教育意义:

These are attempts to come up with regex solutions to toy problems as exercises; they should be educational if you're trying to learn the various ways you can use lookarounds (nesting them, using them to capture, etc):

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

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