user2412672
user2412672

Reputation: 1479

Regex.Matches match uppercase and lowercase

foreach (Match match in Regex.Matches(line, "X"))
{
      indexes.Add(match.Index);
}

I have a quick question. Here is my part of code, and I'm getting indexes of X's, but I also want to get indexes even if X's is in lowercase. What should I write?

Upvotes: 3

Views: 2486

Answers (2)

falsetru
falsetru

Reputation: 369074

Use i mode modifier (make the regular expression case-insensitive):

foreach (Match match in Regex.Matches(line, "(?i)X"))

or use RegexOptions.IgnoreCase option:

foreach (Match match in Regex.Matches(line, "X", RegexOptions.IgnoreCase))

or specify both X and x:

foreach (Match match in Regex.Matches(line, "[Xx]"))

Upvotes: 5

Dan
Dan

Reputation: 10538

Without using regex you could just do Regex.Matches(line.ToLower(), 'x').

Upvotes: 3

Related Questions