正则表达式3-简单搜索
先来看第一种,也是最简单的一种正则表达式 ---- 直接输入字符
原始字符串 Apples are good to your health. 搜索 Apple 正则表达式 Apple
上面的例子中,原始字符串是 Apples are good to your health,如果要搜索 Apple,那么正则表达式中只需要输入 Apple 就可以了。
const string input = "Apples are good to your health."; const string pattern = "Apple"; foreach (Match match in Regex.Matches(input, pattern)) { Console.WriteLine("{0} was found at position {1}.\r\n", match.Value, match.Index); }
请注意,此时是区分大小写的,如果不需要区分,可以将上面的 foreach 改写成:
foreach (Match match in Regex.Matches(input, pattern, RegexOptions.IgnoreCase))