如何将 Regex.Matches 放入数组? [英] How can I put Regex.Matches into an array?

查看:26
本文介绍了如何将 Regex.Matches 放入数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个正则表达式匹配项.如何将它们放入数组并分别调用它们,例如 ID[0] ID[1]?

I have multiple Regex Matches. How can I put them into an array and call them each individually, for example ID[0] ID[1]?

string value = ("{\"ID\":\"([A-Za-z0-9_., ]+)\",");
string ID = Regex.Matches(textt, @value);`

推荐答案

你已经可以做到了,因为 MatchCollection 有一个 int indexer 可让您按索引访问匹配项.这是完全有效的:

You can do that already, since MatchCollection has an int indexer that lets you access matches by index. This is perfectly valid:

MatchCollection matches = Regex.Matches(textt, @value);
Match firstMatch = matches[0];

但如果你真的想把匹配项放到一个数组中,你可以这样做:

But if you really want to put the matches into an array, you can do:

Match[] matches = Regex.Matches(textt, @value)
                       .Cast<Match>()
                       .ToArray();

这篇关于如何将 Regex.Matches 放入数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆