fork(1) download
  1. import re
  2.  
  3. s = '''
  4. COMMENTS:
  5. first comment /user_x
  6. second comment
  7. two lines /user_y
  8. Here is some unimportant text.
  9. KEYWORD:
  10. Don't match comments after keyword like this /user1
  11. '''
  12. pattern = re.compile(r'COMMENTS:\s*((?:.+?/(?:user_x|user_y))+)(?=.+?KEYWORD:)', flags=re.DOTALL)
  13. match = re.search(pattern, s)
  14. if match:
  15. print(match.group(1))
  16.  
  17.  
Success #stdin #stdout 0.02s 27712KB
stdin
Standard input is empty
stdout
first comment /user_x  
second comment
two lines /user_y