How to match regex expression and get precedent words
问题 I use regex to match certain expressions within a text. assume I want to match a number, or numbers separated by commas -including or not spaces-, all within parenthesis in a text. (in reality the matches are more complex including spaces etc) I do the following: import re pattern =re.compile(r"(\()([0-9]+(,)?( )?)+(\))") matches = pattern.findall(content) matches is a list with the matches, for i,match in enumerate(matches): print(i,match) Example text: Lorem ipsum dolor sit amet (12,16) ,