tags:

views:

35

answers:

1

I've got a regex...

internal static readonly Regex _parseSelector = new Regex(@"
    (?<tag>" + _namePattern + @")?
    (?:\.(?<class>" + _namePattern + @"))*
    (?:\#(?<id>" + _namePattern + @"))*
    (?<attr>\[\s*
        (?<name>" + _namePattern + @")\s*
        (?:
            (?<op>[|*~$!^%<>]?=|[<>])\s*
            (?<quote>['""]?)
                (?<value>.*?)
            (?<!\\)\k<quote>\s*
        )?
    \])*
    (?::(?<pseudo>" + _namePattern + @"))*
", RegexOptions.IgnorePatternWhitespace);

For which I grab the match object...

var m = _parseSelector.Match("tag.class1.class2#id[attr1=val1][attr2=\"val2\"][attr3]:pseudo");

Now is there a way to do something akin to m.Group["attr"]["name"]? Or somehow get the groups inside the attr group?

+5  A: 

Group names aren't nested in regular expressions - it's a flat structure. You can just use this:

m.Group["name"]
Mark Byers
I know I can get name directly, but I was hoping I could iterate over all the attributes and *then* get the parts. What am I supposed to do then? How do I loop over the attrs and get the name/value pairs?
Mark
@Mark: I think you are pushing the limit of what it is sensible to do in a single regular expression. Could you first extract the attributes with oen regular expression and then use a second regular expression to further parse them into their components? Or perhaps you could use a parsing library.
Mark Byers
Mark Byers is right: the only way you're going to keep things in sync is to break the attributes down in a second step. You have to iterate through a CaptureCollection to access them anyway, so it's not that big a deal.
Alan Moore
Accepting this because of your comment. That's what I ended up doing, but I guess I was trying to be fancy :P
Mark