Nisho
Nisho

Reputation: 365

C# Regex match multiple words in a string

How can I find all the matches in a string using a regular expression run in C#?

I want to find all matches in the below example string. Example:

inputString: Hello (mail) byebye (time) how are you (mail) how are you (time)

I want to match (mail) and (time) from the example. Including parentheses( and ).

In attempting to solve this, I've writtent the following code.

string testString = @"(mail)|(time)";  

Regex regx = new Regex(Regex.Escape(testString), RegexOptions.IgnoreCase);
List<string> mactches = regx.Matches(inputString).OfType<Match>().Select(m => m.Value).Distinct().ToList();

foreach (string match in mactches)
{
    //Do something
}

Is the pipe(|) used for the logical OR condition?

Upvotes: 9

Views: 23888

Answers (2)

Grant Winney
Grant Winney

Reputation: 66439

Escape the parentheses in your test string:

string testString = @"\(mail\)|\(time\)";

Remove Regex.Escape:

Regex regx = new Regex(testString, RegexOptions.IgnoreCase);

Output (includes parentheses):

(mail)
(time)

The reason Regex.Escape isn't working in your case is that it escapes the | character as well:

Escapes a minimal set of metacharacters (\, *, +, ?, |, {, [, (, ), ^, $, ., #, and whitespace) by replacing them with their \ codes.

Upvotes: 4

FishBasketGordo
FishBasketGordo

Reputation: 23122

Using Regex.Escape(testString) is going to escape your pipe character, turning

@"(mail)|(time)" 

effectively into

@"\(mail\)\|\(time\)".

Thus, your regex is looking for the literal "(mail)|(time)".

If all of your matches are as simple as words surrounded by parens, I would build the regex like this:

List<string> words   = new List<string> { "(mail)", "(time)", ... };
string       pattern = string.Join("|", words.Select(w => Regex.Escape(w)));
Regex        regex   = new Regex(pattern, RegexOptions.IgnoreCase);

Upvotes: 14

Related Questions