简体   繁体   English

如何反转正则表达式组捕获逻辑?

[英]How to invert the regular expression group capture logic?

To create a capturing group in a regex you use (match) and you prefix it with ?: to make it non-capturing, like (?:match) .要在正则表达式中创建捕获组,请使用(match)并在其前面加上?:以使其无法捕获,例如(?:match) The thing is, in any kind of complicated regular expression I find myself wanting to create far more non-capturing groups than capturing ones, so I'd like to reverse this logic and only capture groups beginning with ?: (or whatever).问题是,在任何一种复杂的正则表达式中,我发现自己想要创建的非捕获组比捕获组多得多,所以我想颠倒这个逻辑,只捕获以?: (或其他什么)开头的组。 How can I do this?我怎样才能做到这一点? I mainly use regular expressions with .NET, but I wouldn't mind answers for other languages with regular expressions like Perl, PHP, Python, JavaScript, etc.我主要在 .NET 中使用正则表达式,但我不介意使用正则表达式的其他语言(如 Perl、PHP、Python、JavaScript 等)的答案。

In any language that supports named capture groups you can simply use them for what you want captured, and ignore the numbered ones.在支持命名捕获组的任何语言中,您可以简单地将它们用于您想要捕获的内容,而忽略编号的。

my $string = q(Available from v5.10 in Perl.);

$string =~ /([A-Z].+?)(?<ver>[0-9.]+)\s+(.*?)\./;

say "Version: $+{ver}";

After the regex the capture is available in %+ hash, inside the regex in \\k<name> or \\g{name} .在正则表达式之后,捕获在%+哈希中可用,在正则表达式中的\\k<name>\\g{name}

The downside is that you still capture all that other stuff (what hurts efficiency a little), while the upside is that you still capture all that other stuff (what helps flexibility, if some of it turns needed).缺点是您仍然捕获所有其他内容(这会稍微影响效率),而优点是您仍然捕获所有其他内容(这有助于灵活性,如果需要的话)。

If you want to avoid the clumsiness of (?: ) and turn ( ) groups into non-capturing groups, use the RegexOptions.ExplicitCapture option.如果您想避免(?: )的笨拙并将( )组转换为非捕获组,请使用RegexOptions.ExplicitCapture选项。 Only named groups ( (?<name>subexpression) ) will be captured if this option is being used.如果使用此选项,则只会捕获命名组 ( (?<name>subexpression) )。

However, you cannot turn non-capturing groups (?: ) into capturing groups, unfortunately.但是,不幸的是,您不能将非捕获组(?: )转换为捕获组。

The RegEx constructor as well as other methods from the RegEx class accept RegexOptions flags. RegEx 构造函数以及来自 RegEx 类的其他方法接受RegexOptions标志。

For example:例如:

Regex.Matches(input, pattern, RegexOptions.ExplicitCapture)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM