C# 我是否应该将字符串与正则表达式匹配一起使用
本文关键字:正则表达式 一起 字符串 是否 | 更新日期: 2023-09-27 18:37:24
我有一个文本,我将在该文本中找到匹配项,然后将结果放在数组上。
当我尝试制作字符串数组和字符串变量时,我发现从 MatchCollection 类型更改为字符串类型真的很令人困惑。 我得到错误无法转换.
我应该使用什么类型的数组和变量?以及字符串类型的需求是什么.我只是在使用它,因为我处理字符串。
我想注意,我将在数组之间进行比较以找到常见的匹配项
这是我到目前为止的完整代码C# 正则表达式数组或列表以及哪种类型的字符串或什么?
使用:
string sample = "1a2b3c4d";
MatchCollection matches = Regex.Matches(sample, @"'d");
List<string> matchesList = new List<string>();
foreach (Match match in matches)
matchesList.Add(match.Value);
matchesList.ForEach(Console.WriteLine);
或使用 LINQ:
List<string> matchesList2 = new List<string>();
matchesList2.AddRange(
from Match match in matches
select match.Value
);
matchesList2.ForEach(Console.WriteLine);