使用正则表达式进行字符串求反 [英] String negation using regular expressions

查看:110
本文介绍了使用正则表达式进行字符串求反的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在正则表达式中进行字符串取反?我需要匹配所有不包含字符串".."的字符串.我知道您可以使用^[^\.]*$来匹配所有不包含"."的字符串,但是我需要匹配多个字符.我知道我可以简单地匹配包含".."的字符串,然后取反匹配的返回值以达到相同的结果,但是我只是想知道是否有可能.

Is it possible to do string negation in regular expressions? I need to match all strings that do not contain the string "..". I know you can use ^[^\.]*$ to match all strings that do not contain "." but I need to match more than one character. I know I could simply match a string containing ".." and then negate the return value of the match to achieve the same result but I just wondered if it was possible.

推荐答案

您可以使用否定的提前行:

You can use negative lookaheads:

^(?!.*\.\.).*$

如果表达式可以在字符串中的任意位置找到两个句点的序列,则会导致表达式不匹配.

That causes the expression to not match if it can find a sequence of two periods anywhere in the string.

这篇关于使用正则表达式进行字符串求反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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