正则表达式的字符串不包含多个特定的词 [英] Regex for string not containing multiple specific words

查看:512
本文介绍了正则表达式的字符串不包含多个特定的词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把一个正则表达式来发现,当特定的词的的存在于一个字符串。具体而言,我想知道什么时候主干,标签或分支不存在(这是一个颠覆pre-commit钩子)。根据<一href="http://stackoverflow.com/questions/406230/regular-ex$p$pssion-to-match-string-not-containing-a-word">Regular EX pression匹配的字符串不包含一个字回答一个字使用非查询变通,我可以很容易地做到这一点:

I'm trying to put together a regex to find when specific words don't exist in a string. Specifically, I want to know when "trunk", "tags" or "branches" doesn't exist (this is for a Subversion pre-commit hook). Based on the Regular expression to match string not containing a word answer I can easily do this for one word using negative look-arounds:

^((?!trunk).)*$

这是和经营者,我挣扎,我似乎无法得到组合,包括其他两个词的工作。

It's the "and" operator I'm struggling with and I can't seem to get combinations including the other two words working.

这是运行良好的.NET一个字:

This is running fine in .NET with a single word:

var exp = new Regex(@"^((?!trunk).)*$");
exp.IsMatch("trunk/blah/blah");

,因为它目前为,否则将返回false true,如果主干不会在第二行的路径中。

It will return false as it currently stands or true if "trunk" doesn't exist in the path on the second line.

我是什么在这里失踪?

推荐答案

使用负的先行的断言没有任何某处三个词在输入的:

Use a negative look-ahead that asserts the absence of any of the three words somewhere in the input:

^(?!.*(trunk|tags|branches)).*$

我也稍作整理你的正则表达式来纠正小错误。

I also slightly rearranged your regex to correct minor errors.

这篇关于正则表达式的字符串不包含多个特定的词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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