Search code examples
pythonregexpython-re

How to match a regex expression only if a word is present before or after


I'm really struggling with some regex. I've had a good look at similar questions and I can't work out why it's not working!

I'm trying to match the string 'ok' when it is preceded by 4 digits ((?<=\d{4}\s)ok) but only when the word 'devo' appears anywhere before or anywhere after in the string

text_to_search="devo XXXXXXXXXX 9999 ok ferial blabla" 
pattern = re.compile(r"(?<=devo)((?<=\d{4}\s)ok)|((?<=\d{4}\s)ok)(?=devo)")
matches = pattern.finditer (text_to_search)
[print (x) for x in matches]

This does not return any matches... if i try with:

text_to_search=" XXXXXXXXXX 9999 ok ferial blabla devo" 

it does not work either.

Just for clarity, the two examples above should match, but if i take another example like :

text_to_search=" XXXXXXXXXX 9999 ok ferial blabla"

then this should not produce a match since 'devo' is not present..

Thank you in advance for your help


Solution

  • You have nothing for the anywhere before or after part. Try this

    (?<=devo).*((?<=\d{4}\s)ok)|((?<=\d{4}\s)ok).*(?=devo)
    

    But I think the lookarounds are overkill here. A much simpler

    (devo.*\d{4}\sok)|(\d{4}\sok.*devo)
    

    might work as well.