Helio Santos
Helio Santos

Reputation: 6805

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")

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.

Note that the values on the string I am actually using are random. I cannot use "exe" to build the regex, for example. the "DontMatchMe" is also random.

Upvotes: 1

Views: 222

Answers (2)

Sean
Sean

Reputation: 4470

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:

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. This is probably easier to read and understand, particularly for people not familiar with the more advanced concepts of regular expressions, like negative lookaheads.

Upvotes: 0

Ahmad Mageed
Ahmad Mageed

Reputation: 96497

In order to exclude a specific word, you can use a pattern like this: ^(?!.*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
  • .+: 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));
}

Upvotes: 2

Related Questions