Non-capturing parentheses
Parentheses are used to capture backreferences and also to group strings together (as in the regex (one|two|three)
). This means that any grouping using parentheses also creates a backreference. Sometimes you want to avoid this and not create a backreference. In this case, non-capturing parentheses come in handy. In the example we just saw, the following would group the words, but would not create a backreference:
(?:one|two|three)
The construct (?: )
is what is used to create a non-capturing set of parenthesis. To further show the difference between the two, consider the following regex:
It is (?:hard|difficult) to say goodbye to (.*)
When matched against the string It is hard to say goodbye to you
, this will create a single backreference, which will contain the string you
. The first, non-capturing parentheses also allow strings beginning with It is difficult to say goodbye
to match, but they do not create a backreference.
Non-capturing parentheses are sometimes referred to as "grouping-only parentheses".