[email protected] (Stefan Ram) writes: > Supersedes: <[email protected]> > > MRAB <[email protected]> writes: >> >>> import re >> >>> pattern = r'[0-9]{4,6}' >> >>> pattern2 = r'[0-9][0-9][0-9][0-9]([0-9]){0,2}' >> >>> re.search(pattern, '1234') >><re.Match object; span=(0, 4), match='1234'> >> >>> re.search(pattern2, '1234') >><re.Match object; span=(0, 4), match='1234'> >>They look the same to me. > > |>>> import re > |>>> pattern = r'[0-9]{4,6}' > |>>> pattern2 = r'[0-9][0-9][0-9][0-9]([0-9]){0,2}' > |>>> re.search( pattern, '1234' ).group( 1 ) > |IndexError: no such group > |>>> re.search( pattern2, '1234' ).group( 1 ) > |>>> > The second pattern has parentheses, hence a group. The first doesn't.
-- Piet van Oostrum <[email protected]> WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] -- https://mail.python.org/mailman/listinfo/python-list
