如何使用在C#中运行的正则表达式找到字符串中的所有匹配项?
我想在下面的示例字符串中找到所有匹配项.例:
inputString: Hello (mail) byebye (time) how are you (mail) how are you (time)
Run Code Online (Sandbox Code Playgroud)
我想匹配(mail),并(time)从示例.包括括号(和).
在试图解决这个问题时,我写了下面的代码.
string testString = @"(mail)|(time)";
Regex regx = new Regex(Regex.Escape(testString), RegexOptions.IgnoreCase);
List<string> mactches = regx.Matches(inputString).OfType<Match>().Select(m => m.Value).Distinct().ToList();
foreach (string match in mactches)
{
//Do something
}
Run Code Online (Sandbox Code Playgroud)
pipe(|)用于逻辑OR条件吗?