Interval of integers in regular expression

0

I was solving something and I found this test and I do not understand well what is happening.

Doubt is, it shows me 3456'7 'Why does 7 appear on the exit if I am delimiting [3-6] ?

What is prevailing over the limit that I put in brackets?

sentencia = 'Esto es la prueba 123456789 \n de expresiones regulares en python.'
#sentencia = 'Esto.'
pa = re.compile(r'[3-6]\d\d\d\d')
siesta = pa.finditer(sentencia)
for sies in siesta:
    print(sies)

DEPARTURE:

<_sre.SRE_Match object; span=(20, 25), match='34567'>
Process returned 0 (0x0)        execution time : 0.057 s
Presione una tecla para continuar . . .
    
asked by Mauricio Vega 29.07.2018 в 22:21
source

1 answer

2

[3-6]\d\d\d\d means:

[3-6]      # **Un solo número** que puede ser 3,4,5 o 6
\d\d\d\d   # Cuatro números cualesquiera

So it's normal to find you 34567 since 3 complies with [3-6] and 4567 complies with \ d \ d \ d \ d

To make all the numbers can only go from 3 to 6 you should have done something like this:

[3-6][3-6][3-6][3-6][3-6]

or alternatively [3-6]{5} (means [3-6] repeated 5 times)

    
answered by 29.07.2018 / 22:28
source