c# 我应该使用带有正则表达式匹配的字符串吗
c# should i use string with regex matchs or not
我有一个文本,我将在该文本中找到匹配项,然后将结果放入数组。
当我尝试制作字符串数组和字符串变量时,我发现从 MatchCollection 类型更改为 String 类型真的很令人困惑。我收到错误 cannot convert 。
我应该使用什么类型的数组和变量?
什么是字符串类型的需要。我只是在处理字符串时才使用它。
我要注意我要比较数组以找到共同的匹配项
这是我到目前为止的完整代码
c# regex array or list and which type string or what?
使用:
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);
我有一个文本,我将在该文本中找到匹配项,然后将结果放入数组。
当我尝试制作字符串数组和字符串变量时,我发现从 MatchCollection 类型更改为 String 类型真的很令人困惑。我收到错误 cannot convert 。
我应该使用什么类型的数组和变量? 什么是字符串类型的需要。我只是在处理字符串时才使用它。
我要注意我要比较数组以找到共同的匹配项
这是我到目前为止的完整代码 c# regex array or list and which type string or what?
使用:
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);