This regex is supposed to match any numbers (real or integer - no scientific notation). However, I am not sure what is the use of '?:' inside the parentheses. Could anyone explain this along with some examples? Thank you very much.
Asked
Active
Viewed 1,652 times
2
-
2?: is a non-capturing group. See: http://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group – Chankey Pathak Sep 15 '16 at 05:35
-
While the duplicate mentioned does explain what `?:` does in the regex, the motivation behind using it here should still be explained. – Tim Biegeleisen Sep 15 '16 at 09:33
1 Answers
2
In the regex
?\d+(?:\.\d+)?
The ?:
quantity inside the group in parenthesis instructs the regex engine to not capture the group, which it otherwise would.
By not capturing the quantity in parenthesis, the capture group available (which should be the first one, and the entire expression) would just be the digits occurring before the decimal point, should the number have a fractional component.

Tim Biegeleisen
- 502,043
- 27
- 286
- 360