" + _namePattern + @")? (?...">

C # regex: get sub-capture?

I have 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);

Why am I capturing a matching object ...

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

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

+3
source share
1 answer

Group names are not nested in regular expressions - this is a flat structure. You can simply use this:

m.Group["name"]
+5
source

Source: https://habr.com/ru/post/1767786/


All Articles