在C#中如何使用正则表达式搜索字符串示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication17
{
class Program
{
static void Main(string[] args)
{
string[] str =
{
"Help",
"Chapter 2: Reqiured Help",
"Helpme",
"no match found"
};
string check = "help";
foreach (string ss in str)
{
System.Console.Write("{0,24}", ss);
if (System.Text.RegularExpressions.Regex.IsMatch(ss, check, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
System.Console.WriteLine(" (match for '{0}' found)", check);
}
else
{
System.Console.WriteLine();
}
}
//Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
}
在本文中,我们将讨论如何使用正则表达式在 C# 中搜索字符串。 为此,我们使用位于 System.Text.RegularExpressions 命名空间中的 Regex 类。 这里我们将使用 IsMatch() 方法匹配两个字符串,以便使用正则表达式搜索它们。
日期:2020-04-11 22:50:34 来源:oir作者:oir
