Non-capturing groups in regular expressions
In one of my recent merge requests, someone suggested to use non-capturing group to simplify one of my regular expressions. I haven’t heard of those before, so I hit up the search bar.
Turns out, non-capturing groups use this syntax (?:foobar)
. And unlike normal groups, they donot store reference to the matched content. That makes them perfect when you just want to specify a quantifier for a group.
I am definitely going to start using them often now. It’s moments like these, that make me love code reviews. No matter how much you think know, there is always someone dropping knowledge bombs.
For more regular expression tips, check out my lookbehind assertions example and Mozilla’s Regular Expressions Cheet Sheet.