用于不包含多个特定单词的字符串的正则表达式

时间:2022-09-13 16:35:43

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:

我试图将一个regex组合在一起,以便在字符串中不存在特定的单词时找到它。具体地说,我想知道何时“主干”、“标记”或“分支”不存在(这是用于Subversion预提交挂钩的)。基于匹配不包含单词答案的字符串的正则表达式,我可以很容易地对一个单词使用否定查找框:

^((?!trunk).)*$

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

这是我一直在纠结的“和”运算符,我似乎无法将其他两个词组合在一起。

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

这在。net中运行得很好,只有一个词:

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

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

如果在第二行路径中不存在“trunk”,则返回false。

What am I missing here?

我错过了什么?

2 个解决方案

#1


58  

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.

我也稍微调整了你的正则表达式来修正小错误。

#2


3  

Use a "standard" match and look for !IsMatch

使用“标准”匹配并查找!IsMatch

var exp = new Regex(@"trunk|tags|branches");
var result = !exp.IsMatch("trunk/blah/blah");

Why persons love to make their life difficult?

为什么人们喜欢让他们的生活变得困难?

Ah... And remember the ass principle! http://www.codinghorror.com/blog/2008/10/obscenity-filters-bad-idea-or-incredibly-intercoursing-bad-idea.html

啊…记住屁股原则!http://www.codinghorror.com/blog/2008/10/obscenity-filters-bad-idea-or-incredibly-intercoursing-bad-idea.html

So it would be better to write

所以最好还是写下来

var exp = new Regex(@"\b(trunk|tags|branches)\b");

But if you really need a negative lookahed expression, and keeping up with the ass principle

但是,如果你真的需要一个负面的表情,并跟上屁股原则

var exp = new Regex(@"^(?!.*\b(trunk|tags|branches)\b)";

Tester: http://gskinner.com/RegExr/?2uv1g

测试人员:http://gskinner.com/RegExr/?2uv1g

I'll note that if you are looking for full paths (words separated by / or \) then

我将注意到,如果您正在寻找完整路径(由/或\分隔的单词),那么

var exp = new Regex(@"^(?!.*(^|\\|/)(trunk|tags|branches)(/|\\|$))";

Tester: http://gskinner.com/RegExr/?2uv1p

测试人员:http://gskinner.com/RegExr/?2uv1p

#1


58  

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.

我也稍微调整了你的正则表达式来修正小错误。

#2


3  

Use a "standard" match and look for !IsMatch

使用“标准”匹配并查找!IsMatch

var exp = new Regex(@"trunk|tags|branches");
var result = !exp.IsMatch("trunk/blah/blah");

Why persons love to make their life difficult?

为什么人们喜欢让他们的生活变得困难?

Ah... And remember the ass principle! http://www.codinghorror.com/blog/2008/10/obscenity-filters-bad-idea-or-incredibly-intercoursing-bad-idea.html

啊…记住屁股原则!http://www.codinghorror.com/blog/2008/10/obscenity-filters-bad-idea-or-incredibly-intercoursing-bad-idea.html

So it would be better to write

所以最好还是写下来

var exp = new Regex(@"\b(trunk|tags|branches)\b");

But if you really need a negative lookahed expression, and keeping up with the ass principle

但是,如果你真的需要一个负面的表情,并跟上屁股原则

var exp = new Regex(@"^(?!.*\b(trunk|tags|branches)\b)";

Tester: http://gskinner.com/RegExr/?2uv1g

测试人员:http://gskinner.com/RegExr/?2uv1g

I'll note that if you are looking for full paths (words separated by / or \) then

我将注意到,如果您正在寻找完整路径(由/或\分隔的单词),那么

var exp = new Regex(@"^(?!.*(^|\\|/)(trunk|tags|branches)(/|\\|$))";

Tester: http://gskinner.com/RegExr/?2uv1p

测试人员:http://gskinner.com/RegExr/?2uv1p