?: Notation in Regular Expression

Regex

Regex Problem Overview


for one of my classes I have to describe the following regular expression:

\b4[0-9]{12}(?:[0-9]{3})\b

I understand that it selects a number that: begins with 4, is followed by 12 digits (each between 0-9), and is followed by another 3 digits.

What I don't understand is the the question mark with the semicolon (?:....). I've tried looking online to find out what this means but the links I've found were somewhat confusing; I was hoping someone could give me a quick basic idea of what the question mark does in this example.

Regex Solutions


Solution 1 - Regex

This is going to be short answer.

When you use (?:) it means that the group is matched but is not captured for back-referencing i.e non-capturing group. It's not stored in memory to be referenced later on.

For example:

(34)5\1

This regex means that you are looking for 34 followed by 5 and then again 34. Definitely you could write it as 34534 but sometimes the captured group is a complex pattern which you could not predict before hand.

So whatever is matched by capturing group should be appearing again.

Regex101 demo for back-referencing


Back-referencing is also used while replacement.

For Example:

([A-Z]+)[0-9]+

This regex will look for many upper case letters followed by many digits. And I wish to replace this whole pattern just by found upper case letters.

Then I would replace whole pattern by using \1 which stands for back-referencing first captured group.

Regex101 demo for replacement

If you change to (?:[A-Z]+)[0-9]+ this will no longer capture it and hence cannot be referenced back.

Regex101 demo for non-capturing group

A live answer.

Solution 2 - Regex

It's called a 'non-capturing group', which means the regex would not make a group by the match inside the parenteses like it would otherwise do (normally, a parenthesis creates a group).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionr-g-s-View Question on Stackoverflow
Solution 1 - Regexuser2705585View Answer on Stackoverflow
Solution 2 - RegexPoul BakView Answer on Stackoverflow