9

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?

2 Answers 2

14

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);
Sign up to request clarification or add additional context in comments.

Comments

4

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.

5 Comments

Wow... correct. but why is the use of Escape than? because i want to build the testString dynamically from a list of words for example: (mail) (time) (this) (that) I thought i could escape them useing the Regex.Escape. Weird! @Grant Winney
I think because it ends up escaping your | character too.
@Nisho, use Escape by all means, something like String.Join("|", myListOfWords.Select(Regex.Escape)).
Feel free to use this in your answer if you believe it adds value :)
@decPL Looks like FishBasketGordo had the same thought as you. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.