What does (?: do in a regular expression
It's a non-capture group, which essentially is the same as using (...)
, but the content isn't retained (not available as a back reference).
If you're doing something like this: (abc)(?:123)(def)
You'll get abc
in $1
and def
in $2
, but 123
will only be matched.
(?:)
creates a non-capturing group. It groups things together without creating a backreference.
A backreference is a part you can refer to in the expression or a possible replacement (usually by saying \1
or $1
etc - depending on flavor). You can also usually extract them from a match afterwards when using regex in a programming language. The only reason for using (?:)
is to avoid creating a new backreference, which avoids incrementing the group number, and saves (a usually negligible amount of) memory