fork(3) download
  1. import re
  2. REMOVE_LIST = ["(A mass = 200 GeV)", "More+[fun]+text"]
  3.  
  4. remove_with_boundaries = '|'.join([re.escape(x) for x in REMOVE_LIST if re.match(r'\w', x) and re.search(r'\w$', x)])
  5. remove_with_no_boundaries = '|'.join([re.escape(x) for x in REMOVE_LIST if not re.match(r'\w', x) and not re.search(r'\w$', x)])
  6. remove_with_right_boundaries = '|'.join([re.escape(x) for x in REMOVE_LIST if not re.match(r'\w', x) and re.search(r'\w$', x)])
  7. remove_with_left_boundaries = '|'.join([re.escape(x) for x in REMOVE_LIST if re.match(r'\w', x) and not re.search(r'\w$', x)])
  8.  
  9. ptrn = ''
  10. if len(remove_with_boundaries) > 0:
  11. ptrn += r'\b(?:'+remove_with_boundaries+r')\b'
  12. if len(remove_with_left_boundaries) > 0:
  13. ptrn += r'|\b(?:' + remove_with_left_boundaries + r')'
  14. if len(remove_with_right_boundaries) > 0:
  15. ptrn += r'|(?:' + remove_with_right_boundaries + r')\b'
  16. if len(remove_with_no_boundaries) > 0:
  17. ptrn += r'|(?:' + remove_with_no_boundaries + r')'
  18.  
  19. print ptrn
  20. regex = re.compile(ptrn)
  21. print regex.findall("Now, (A mass = 200 GeV) and More+[fun]+text inside")
  22.  
Success #stdin #stdout 0.01s 7736KB
stdin
Standard input is empty
stdout
\b(?:More\+\[fun\]\+text)\b|(?:\(A\ mass\ \=\ 200\ GeV\))
['(A mass = 200 GeV)', 'More+[fun]+text']