Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does .NET Regex support global matching?

Tags:

c#

.net

regex

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?

like image 682
Dave Avatar asked Apr 26 '10 17:04

Dave


People also ask

What is .NET regex?

The regular expression pattern to identify in the text. In . NET, regular expression patterns are defined by a special syntax or language, which is compatible with Perl 5 regular expressions and adds some additional features such as right-to-left matching.

What regex does C# use?

In C#, Regular Expression is a pattern which is used to parse and check whether the given input text is matching with the given pattern or not. In C#, Regular Expressions are generally termed as C# Regex. The . Net Framework provides a regular expression engine that allows the pattern matching.

Is regex case sensitive?

By default, the comparison of an input string with any literal characters in a regular expression pattern is case-sensitive, white space in a regular expression pattern is interpreted as literal white-space characters, and capturing groups in a regular expression are named implicitly as well as explicitly.

What is regex in VB net?

A regular expression is a pattern that could be matched against an input text. The . Net framework provides a regular expression engine that allows such matching. A pattern consists of one or more character literals, operators, or constructs.


1 Answers

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

like image 193
SLaks Avatar answered Sep 19 '22 15:09

SLaks