I want to match characters including and between two strings. (for a given number of words acceptable between them)
For example:
text = 'I want apples and oranges'
Parameters are 'apples'
, 'oranges'
and k=2
, which is the max allowable words in between these strings words. I am expecting the output to be 'apples and oranges'
because there is only one word between the two given strings
This is very similar to the (?<=...)
pattern in regex but I am unable to define the number of acceptable words in between and I want the relevant text extracted instead of just what's in between
What I have now:
import re
text = 'I want apples and oranges'
pattern = "(?<=apples)(.*)(?=oranges)"
m = re.search(pattern, text)
print(m)
<re.Match object; span=(13, 18), match=' and '>
This outputs ' and '
. But I want to get the output of apples and oranges
, instead of only whats in between. And I want to be able to limit the number of acceptable words between apples and oranges. For example, if I define k = 2
and if the sentence is "I want apples along with some oranges" this should not match because there are 3 words between apples and oranges.
Does anyone know if I can do this with regex as well?