正则表达式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))

好了,赶紧在 VisualStudio 中试一下吧!

posted @ 2018-09-06 11:04  优秀程序缘  阅读(98)  评论(0编辑  收藏  举报