r...@zedat.fu-berlin.de (Stefan Ram) writes:

> Supersedes: <group-20190917042...@ram.dialup.fu-berlin.de>
>
> MRAB <pyt...@mrabarnett.plus.com> 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 <pie...@vanoostrum.org>
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to