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

Time:02-18

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".

CodePudding user response:

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

CodePudding user response:

With regex:

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

Try it online!

  • Related