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.
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