How to check if a named capture group exists?

后端 未结 1 933
悲&欢浪女
悲&欢浪女 2020-12-17 08:48

In my regex the pattern is something like this:

@\"Something\\(\\d+, \"\"(.+)\"\"(, .{1,5}, \\d+, (?\\d+)?\\),\"

So I would

相关标签:
1条回答
  • 2020-12-17 09:42

    According to the documentation:

    If groupname is not the name of a capturing group in the collection, or if groupname is the name of a capturing group that has not been matched in the input string, the method returns a Group object whose Group.Success property is false and whose Group.Value property is String.Empty.

    var regex = new Regex(@"Something\(\d+, ""(.+)""(, .{1,5}, \d+, (?<somename>\d+)?\),");
    var match = regex.Match(input);
    var group = match.Groups["somename"];
    bool exists = group.Success;
    
    0 讨论(0)
提交回复
热议问题