RegEx: difference between "(?:...) and normal parentheses

Posted by N0thing on Stack Overflow See other posts from Stack Overflow or by N0thing
Published on 2013-10-20T21:39:22Z Indexed on 2013/10/20 21:54 UTC
Read the original article Hit count: 219

Filed under:
>>> re.findall(r"(?:do|re|mi)+", "mimi")
['mimi']
>>> re.findall(r"(do|re|mi)+", "mimi")
['mi']

According to my understanding of the definitions, it should produce the same answer. The only difference between (...) and (?:...) should be whether or not we can use back-references later. Am I missing something?

(...)

Matches whatever regular expression is inside the parentheses, and indicates the start and end of a group; the contents of a group can be retrieved after a match has been performed, and can be matched later in the string with the \number special sequence, described below. To match the literals '(' or ')', use ( or ), or enclose them inside a character class: [(] [)].

(?:...)

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.

© Stack Overflow or respective owner

Related posts about regex