Regex: coincide con la secuencia de una cadena no determinada [duplicado]

s = 'blah blah blah... _ABC_superman_is_cool_CBA_ ...blah blah blah...'

This is just an example, but I want to match everything between _ABC_ and _CBA_. So 'superman_is_cool'. There may be multiple sections of _ABC_..._CBA_.

re.findall('_ABC_(.*)(?=_CBA_)', s)

I tried this first, but obviously doesn't correctly work at all.

preguntado el 09 de enero de 11 a las 06:01

2 Respuestas

I added an additional _ABC_, _CBA_ pair to make sure it finds all the matches:

>>> s = 'blah blah blah... _ABC_superman_is_cool_CBA_ ...blah blah _ABC_blah_CBA_...'
>>> re.findall('_ABC_(.*?)_CBA_', s)
['superman_is_cool', 'blah']

La ? hace que el * operator non-greedy so it finds as short a match as possible. Without it the result would be ['superman_is_cool_CBA_ ...blah blah _ABC_blah'].

Respondido el 09 de enero de 11 a las 09:01

+1 for not getting bitten by greediness ;) - user395760

Prueba esta

re.findall('_ABC_.*_CBA_)', s)

Respondido el 09 de enero de 11 a las 09:01

That's not a valid regular expression.. - jairajs89

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.