In my regex the pattern is something like this:
在我的正则表达式中,模式是这样的:
@"Something\(\d+, ""(.+)""(, .{1,5}, \d+, (?<somename>\d+)?\),"
So I would like to know if <somename>
exists. If it was a normal capture group, I could just check if the capture groups are greater than the number of groups without that/those capture group(s), but I don't have the option here.
所以我想知道
Could anyone help me find a way round this? I don't need it to be efficient, it's just for a one-time program that's used for sorting, so I don't mind if it takes a bit to run. It's not going to be for public code.
有人能帮我找到解决方法吗?我不需要它有效率,它只是用于排序的一次性程序,所以我不介意它是否需要运行一点。它不适用于公共代码。
1 个解决方案
#1
25
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.
如果groupname不是集合中捕获组的名称,或者groupname是输入字符串中未匹配的捕获组的名称,则该方法返回Group对象,其Group.Success属性为false且其Group .Value属性是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;
#1
25
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.
如果groupname不是集合中捕获组的名称,或者groupname是输入字符串中未匹配的捕获组的名称,则该方法返回Group对象,其Group.Success属性为false且其Group .Value属性是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;