fork download
  1. import re
  2.  
  3. strings = [
  4. "home travel or home visit or home flight",
  5. "travel or visit or flight"
  6. ]
  7. pattern = r"\b((?:home|parents)\s+(?:flight|visit|travel))\b|\b(?:flight|visit|travel)\b"
  8. for s in strings:
  9. print(
  10. re.sub(
  11. pattern,
  12. lambda x: x.group(1) if x.group(1) else "[replacement here]",
  13. s
  14. )
  15. )
Success #stdin #stdout 0.03s 9448KB
stdin
Standard input is empty
stdout
home   travel or home   visit or home    flight
[replacement here] or [replacement here] or [replacement here]