如何从MatchCollection中获取唯一的元素

3 c# regex collections

我知道总有一个元素MatchCollection:

Regex reg = new Regex("1234");
MatchCollection matches = reg.Matches("fjasij 1234 gdsgds");
Console.WriteLine(matches.Count);
string s = ?;
Run Code Online (Sandbox Code Playgroud)

如何在string s 没有foreach循环的情况下将这一个元素分配给变量?

Sim*_*ead 10

为什么要使用MatchCollection呢?得到一个Match:

var match = reg.Match("fjasij 1234 gdsgds");
Run Code Online (Sandbox Code Playgroud)


Sys*_*own 6

string s = matches[0];
Run Code Online (Sandbox Code Playgroud)

请注意,如果匹配为零,则会失败.