not working
Piet van Oostrum
piet-l at vanoostrum.org
Tue Sep 17 07:25:27 EDT 2019
ram at zedat.fu-berlin.de (Stefan Ram) writes:
> Supersedes: <group-20190917042528 at ram.dialup.fu-berlin.de>
>
> MRAB <python at 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 <piet-l at vanoostrum.org>
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
More information about the Python-list
mailing list