Regex.Matches匹配大写和小写

时间:2014-01-16 16:27:33

标签: .net regex

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

我有一个简单的问题。这是我的代码部分,我正在获取X的索引,但我也希望得到索引,即使X是小写的。我该怎么写?

2 个答案:

答案 0 :(得分:4)

使用i模式修饰符(使正则表达式不区分大小写):

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

或使用RegexOptions.IgnoreCase选项:

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

或同时指定Xx

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

答案 1 :(得分:3)

如果不使用正则表达式,您只需执行Regex.Matches(line.ToLower(), 'x')

相关问题