Regex match multiple per line

D

DKode

I have the following regex that I am using:

Match match = Regex.Match(global, "%(?<column>.+?)%");

my input string (global) is this:
"something\%username%\somethingelse\%match2%\some\%match3%

I want it to match "username","match2" and "match3"

but it only matches %username%, it doesnt continue on and get the other
matches

match.Groups.Count is equal to 2, which i assume, 0 is the whole string
and 1 is "%username%

How do I get it so it matches all of the strings between %%

thanks

dkode
 
C

Chris R. Timmons

I have the following regex that I am using:

Match match = Regex.Match(global, "%(?<column>.+?)%");

my input string (global) is this:
"something\%username%\somethingelse\%match2%\some\%match3%

I want it to match "username","match2" and "match3"

but it only matches %username%, it doesnt continue on and get
the other matches

match.Groups.Count is equal to 2, which i assume, 0 is the whole
string and 1 is "%username%

How do I get it so it matches all of the strings between %%

dkode,

Use a MatchCollection object and the Regex.Matches method:


MatchCollection matches = Regex.Matches(global, "%(?<column>.+?)%");

foreach (Match m in matches)
Console.WriteLine(m.Groups["column"].ToString());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top