Does .NET Regex support global matching?

C#.NetRegex

C# Problem Overview


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*

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?

C# Solutions


Solution 1 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaveView Question on Stackoverflow
Solution 1 - C#SLaksView Answer on Stackoverflow