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 after keyword like this /user1 '''
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) OUTPUT: first comment /user_x second comment in two lines /user_y Here is some unimportant text.
What am I doing wrong? Thanks a lot