I would like to match any user comment until KEYWORD. Also I would like to skip variable unimportant text after last comment before keyword.
import re` string = ''' COMMENTS: first comment /user_x second comment two lines /user_y Here is some unimportant text. KEYWORD: Don't match comments or anything else after first keyword like this /user_x KEYWORD: <- again Also ignore same keyword which could appear serveral times. '''
My result doesn't skip the unimportant text.
pattern = re.compile(r'(?<=COMMENTS:)(.+?/(user_x|user_y))+?(?:.+?)(?=KEYWORD:)', flags=re.DOTALL) match = re.search(pattern, string).group(0) print(match)
I would like to have the following output:
first comment /user_x second comment in two lines /user_y
What am I doing wrong? Thanks a lot