fork(1) download
  1. import re
  2. text = 'I want apples and oranges'
  3. k = 2
  4. pattern = f"apples(?:\s+\w+){{0,{k}}}\s+oranges"
  5. m = re.search(pattern, text)
  6. if m:
  7. print(m.group())
Success #stdin #stdout 0.02s 9576KB
stdin
Standard input is empty
stdout
apples and oranges