tags:

views:

108

answers:

1

Hi friends,

I have a string:

type_name "abc" < text1 > text2 >
> "ab123" < text3

Now I want to extract all alphanumeric words which are preceded by a "<" or ">"

So I wrote:

[<>]\s*(?'name'\w+)

I'm getting the matches, (and for example above I get 3 matches, each has a group called name) and in name I'm able to access the values text1, text2 and text3 namely. But I want them to come in the Same group but a different Capture, so that I can write something like

foreach(Capture C in Match.Groups["name"])

I need it so because of the way I've designed my parser to process regexes, it expects different values in the same group but a different capture. Can you help me as how to get them together.

A: 

You don't really want them in the same match because you'd have to account for stuff you didn't want in the rest of input and it'd be slower. You're better off doing something like this:

Regex re = new Regex(@"[<>]\s*(?<name>\w+)");
MatchCollection matches = re.Matches("type_name \"abc\" < text1 > text2 >  \"ab123\" < text3");            
foreach (Match m in matches)
{
   string name = m.Groups["name"].Value;
}
Jeff Moser