Regular Expressions in Python
Maria Eugenia Inzaugarat
Data Scientist


Tại vị trí hiện tại trong tiến trình khớp, hãy nhìn trước hoặc sau và kiểm tra xem một mẫu có khớp hay không trước khi tiếp tục.


my_text = "tweets.txt transferred, mypass.txt transferred, keywords.txt error"re.findall(r"\w+\.txt ", my_text)
my_text = "tweets.txt transferred, mypass.txt transferred, keywords.txt error"re.findall(r"\w+\.txt(?=\stransferred)", my_text)
['tweets.txt', 'mypass.txt']
my_text = "tweets.txt transferred, mypass.txt transferred, keywords.txt error"re.findall(r"\w+\.txt ", my_text)
my_text = "tweets.txt transferred, mypass.txt transferred, keywords.txt error"re.findall(r"\w+\.txt(?!\stransferred)", my_text)
['keywords.txt']


my_text = "Member: Angus Young, Member: Chris Slade, Past: Malcolm Young, Past: Cliff Williams."re.findall(r" \w+\s\w+", my_text)
my_text = "Member: Angus Young, Member: Chris Slade, Past: Malcolm Young, Past: Cliff Williams."re.findall(r"(?<=Member:\s)\w+\s\w+", my_text)
['Angus Young', 'Chris Slade']
my_text = "My white cat sat at the table. However, my brown dog was lying on the couch."re.findall(r"(?<!brown\s)(cat|dog)", my_text)
['cat']
Regular Expressions in Python