I am wondering what is the point of using non-capturing groups.
I tested a bunch of simple regexes on regex101.com and found that whether using a non-capturing group or not does not effect the number of steps it takes to capture the string.
For example,
fo(o)
takes 6 steps to match foo
.
fo(?:o)
also takes 6 steps.
I know that using a non-capturing group means that there will be one fewer group in the match result, but so what? I can still count which group the text I want is in and get that group. The group number I need may be different, but I can still get the group either way.
For example:
f(o)+(.)\2+
is practically the same as
f(?:o)+(.)\1+
After reading this post, I learnt that with a non-capturing group, the regex engine does not "capture" the characters matched in a non-capturing group by putting the characters into an array. However, isn't that little bit of space to store the captured string negligible?
What is an example of a regex with capturing groups that is/behaves significantly different if non-capturing groups were used instead? Alternatively, what is an example of a case where non-capturing groups must be used?
P.S. I don't think the regex flavour matters here, since every main regex flavour's non-capturing and capturing groups behave the same (I think). If I'm wrong, answer in python will be fine.