osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

not working


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]