Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of `?:` in regular expression

Tags:

regex

I saw a regular expression to match a URL: /^\/users?(?:\/(\d+)(?:\.\.(\d+))?)?/. I am confused by the usage of ?: in the beginning of each group match.

What's the meaning of that?

like image 555
steveyang Avatar asked Jun 28 '12 10:06

steveyang


People also ask

What is ?: In regex?

It indicates that the subpattern is a non-capture subpattern. That means whatever is matched in (?:\w+\s) , even though it's enclosed by () it won't appear in the list of matches, only (\w+) will.

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .


2 Answers

(?:) (the () are part of the expression) is a non-capturing group.

See http://www.regular-expressions.info/refadv.html.

like image 95
deceze Avatar answered Oct 14 '22 12:10

deceze


It's a non-capturing group, so if a match is made that particular group will not be captured.

http://www.regular-expressions.info/refadv.html

like image 32
billyonecan Avatar answered Oct 14 '22 11:10

billyonecan