是否.NET正则表达式支持全局匹配?全局、正则表达式、NET

2023-09-03 04:42:48 作者:  旧光影里的子少年

我一直没能在网上找到有关的事。有RegexOptions,但它不具有全局作为它的一个选项。内联修饰符名单中还没有提到全局匹配。

I haven't been able to find anything online regarding this. There's RegexOptions, but it doesn't have Global as one of its options. The inline modifiers list also doesn't mention global matching.

在简单地说,我有一个正则表达式解析像

In a nutshell, I've got a regex to parse something like

--arga= "arg1"  --argb ="arg2" 

成单独的参数名称/使用此正则表达式值对:

into separate argument name/value pairs using this regex:

--(\\w+)\\s*=\\s*\"(\\w+)\"\\s*

但.NET正则表达式类并没有在全球范围做(反复)。因此,为了让我得到这个工作,我不得不做一个匹配,然后从参数字符串中删除这一点,循环一遍又一遍,直到我已经用尽了所有的参数。

but the .NET Regex class doesn't do it globally (iteratively). So in order for me to get this to work, I'd have to do a match, then remove this from the argument string, and loop over and over again until I've exhausted all of the arguments.

这将是更好的运行正则表达式一次,然后遍历匹配组得到的名称值对。这可能吗?我在想什么?

It would be nicer to run the regex once, and then loop over the match groups to get the name value pairs. Is this possible? What am I missing?

推荐答案

您正在寻找的Regex.Matches方法(复数),返回包含原始字符串中的所有匹配的集合。

You're looking for the Regex.Matches method (plural), which returns a collection containing all of the matches in the original string.