简体   繁体   English

正则表达式组匹配没有单词边界

[英]Regex group matching without word boundaries

I am trying to create a function that returns true when the string doesn't have a particular group of chars (in this example the group is "DontMatchMe") 我正在尝试创建一个在字符串没有特定字符组时返回true的函数(在此示例中,该组是“ DontMatchMe”)

so, of the following examples: 因此,以下示例:

example1  
examDontMatchMeple2  
example3  
examDontMatchMeple4  
example4  

valid matches are: 有效的匹配项是:

example1  
example3  
example4  

my first option was to use the pattern .*(?!DontMatchMe).* but .* is consuming everything, the match is always true. 我的第一个选择是使用.*(?!DontMatchMe).*但是.*正在消耗所有内容,匹配始终为true。

Note that the values on the string I am actually using are random. 请注意,我实际使用的字符串上的值是随机的。 I cannot use "exe" to build the regex, for example. 例如,我不能使用“ exe”来构建正则表达式。 the "DontMatchMe" is also random. “ DontMatchMe”也是随机的。

In order to exclude a specific word, you can use a pattern like this: ^(?!.*DontMatchMe).+ 为了排除特定的单词,您可以使用如下模式: ^(?!.*DontMatchMe).+

To avoid the issue with .* consuming everything you can anchor the pattern to the beginning of the string. 为了避免.*占用所有内容的问题,您可以将模式锚定到字符串的开头。 The pattern break-down is as follows: 模式细分如下:

  • ^ : anchor to the beginning of the string ^ :锚定到字符串的开头
  • (?!.*DontMatchMe) : negative look-ahead that matches any character and the text to be ignored (?!.*DontMatchMe) :匹配任何字符和要忽略的文本的否定超前
  • .+ : finally, match one or more characters (which would happen as long as the look-ahead didn't match anything) .+ :最后,匹配一个或多个字符(只要前瞻不匹配任何字符,就会发生)

Example: 例:

string[] inputs = 
{
    "example1",
    "examDontMatchMeple2",
    "example3",
    "examDontMatchMeple4",
    "example4"
};

string ignoreText = "DontMatchMe";
string pattern = String.Format("^(?!.*{0}).+", Regex.Escape(ignoreText));

foreach (var input in inputs)
{    
    Console.WriteLine("{0}: {1}", input, Regex.IsMatch(input, pattern));
}

If it's a simple non-regex string that you want to check for, you can simply use the Contains method and invert the result: 如果您要检查的是一个简单的非正则表达式字符串,则可以简单地使用Contains方法并反转结果:

bool doesNotContain(string s, string group) {
    // error check for nulls first (not included here)
    return !s.Contains(group);
}

If you want your group to possibly be a regex, you can still use the same principle. 如果希望您的组成为正则表达式,则仍然可以使用相同的原理。 Look for the pattern you don't want, and if it's there return false, otherwise return true. 查找您不需要的模式,如果存在,则返回false,否则返回true。 This is probably easier to read and understand, particularly for people not familiar with the more advanced concepts of regular expressions, like negative lookaheads. 这可能更容易阅读和理解,特别是对于不熟悉正则表达式等更高级概念(例如否定的先行者)的人们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM