c# - 如何从 MatchCollection 中获取唯一元素

标签 c# regex collections

我知道 MatchCollection 中始终只有一个元素:

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

如何在没有foreach循环的情况下将这个元素赋值给变量string s

最佳答案

为什么要使用 MatchCollection 呢?只需获取一个 Match:

var match = reg.Match("fjasij 1234 gdsgds");

关于c# - 如何从 MatchCollection 中获取唯一元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21126569/

相关文章:

c# - 在WPF MVVM中按标签查找标签

javascript - 如何选择末尾没有点的段落并删除 - 使用 jQuery/javascript?

python - 从 Python 迁移到 Racket(正则表达式库和 "Racket Way")

java - 使用什么容器来保持索引自动更新?

java - 有什么不同

c# - 使用哪个 C# 集合代替 List<KeyValuePair<string, double>>?

c# - 如何在wpf中使按钮和背景闪烁

c# - 由于错误 "Reference to undeclared entity ' nbsp',XmlDocument 无法加载 XHTML 字符串”

c# - 如何在 Web API Controller 中接收字节数组和 json

java - 正则表达式只允许 Java 字符串中出现一个标点符号