'How can I get a word that I don't know but knowing the words after and before?

I have a string like

" 89 o/t rep hi my name is hi my helo, 123, A, 89 o/d rep 129 / 314 judge asdfff"

I want to get the 129 / 314 knowing that the word after it is always "judge" and the words before are "89 o/d rep".



Solution 1:[1]

before = '89 o/d rep'
after = 'judge'
b = s.find(before)
a = s.find(after)
print(s[b+len(before):a])

Solution 2:[2]

With regex:

re.search("89 o/d rep (.*?) judge", s)[1]

Try it online!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Roberts
Solution 2