steveyang
steveyang

Reputation: 9298

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

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?

Upvotes: 8

Views: 4889

Answers (3)

pyfunc
pyfunc

Reputation: 66739

Read through: http://docs.python.org/library/re.html

(?:...)

A non-capturing version of regular parentheses. Matches whatever regular expression is inside the parentheses, but the substring matched by the group cannot be retrieved after performing a match or referenced later in the pattern.

Upvotes: 1

deceze
deceze

Reputation: 522597

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

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

Upvotes: 10

billyonecan
billyonecan

Reputation: 20270

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

Upvotes: 5

Related Questions