55

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.

Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
r-g-s-
  • 591
  • 1
  • 4
  • 5
  • 1
    ?: is used to denote non capturing group. You can refer http://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group – firec Apr 10 '16 at 01:01

2 Answers2

90

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.

Community
  • 1
  • 1
15

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).

Sebastian Lenartowicz
  • 4,695
  • 4
  • 28
  • 39
Poul Bak
  • 10,450
  • 5
  • 32
  • 57