开发者

C# Regex: Get sub-capture?

I've got a regex...

internal static readonly Regex _parseSelector = new Regex(@"
    (?<tag>" + _namePattern + @")?
    (?:\.(?<class>" + _namePattern + @"))*
    (?:\#(?<id>" + _namePattern + @"))*
    (?<attr>\[\s*
        (?<name>" + _namePattern + @")\s*
        (?:
            (?<op>[|*~$!^%<开发者_StackOverflow社区>]?=|[<>])\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?


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

m.Group["name"]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜