Extracting the exact word between two words without question mark in python using REGEX

Viewed 49

I am trying to extract a word between two specific words in a string.

Here is what I have tried so far,

import re
st = 'did this something?Yessomething'
re.findall('did this something?(.*?)something', st)
Current Output: ['?Yes']
Desired Output: ['Yes']

I can replace the question mark with a blank space to get the desired output but I am looking for the way to extract the exact word after the question mark and the next word.

1 Answers

You need to escape ? after something.

Do it as follows:

print(re.findall('did this something\\?(.*?)something', st))

Output:

['Yes']
Related