Home:ALL Converter>How to invert the regular expression group capture logic?

How to invert the regular expression group capture logic?

Ask Time:2018-11-13T05:37:24         Author:Jez

Json Formatter

To create a capturing group in a regex you use (match) and you prefix it with ?: to make it non-capturing, like (?: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.

Author:Jez,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53270459/how-to-invert-the-regular-expression-group-capture-logic
user2819245 :

If you want to avoid the clumsiness of (?: ) and turn ( ) groups into non-capturing groups, use the RegexOptions.ExplicitCapture option. Only named groups ((?<name>subexpression)) will be captured if this option is being used.\n\nHowever, you cannot turn non-capturing groups (?: ) into capturing groups, unfortunately.\n\nThe RegEx constructor as well as other methods from the RegEx class accept RegexOptions flags.\n\nFor example:\n\nRegex.Matches(input, pattern, RegexOptions.ExplicitCapture)\n",
2018-11-12T21:44:03
yy