tags:

views:

51

answers:

1

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?

+3  A: 

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

SLaks
ugh... thanks! I completely missed that. I will give it a shot now.
Dave
can you recommend the cleanest way to get at the name value pairs? When I use Matches, it returns two groups: `--arga="arg1"` and `--argb="arg2"`, but I was hoping for these, **plus** `arga`, `arg1`, `argb`, and `arg2`.
Dave
Check the `Groups[1]` property of each `Match` object.
SLaks
<slaps himself> of course... :)
Dave
Hah, funny comments :)
Stormenet