1.后向引用
pattern = re.compile(r"(\w+)")#['hello', 'go', 'go', 'hello']
# pattern = re.compile(r"\b(\w+)\b\s+\b")#['hello', 'go', 'go']
# pattern = re.compile(r"\b(\w+)\b\s+\1\b")#['go'] 匹配重复的单词
str = 'hello hello go go come come go go hello hello'
pattern = re.compile(r"\b(?P<word>\w+)\b\s+(?P=word)\b")
print(re.findall(pattern,str))
2.零宽断言
str = "I'm singing while you're dancing."
pattern = re.compile(r"\b\w+(?=ing\b)")#(?=exp)匹配exp前面的位置
print(re.findall(pattern,str))#['sing', 'danc']
str = 'reading a book'
pattern = re.compile(r"(?<=\bre)\w+\b")#(?<=exp)匹配exp后面的位置
print(re.findall(pattern,str))#['ading']
str = '123,456,789'
pattern = re.compile(r"(?<=,)?(\d+)(?=,)?")#匹配以逗号相隔的数字
print(re.findall(pattern,str))#['123','456','789']
3.负向零宽断言
str = '<span> hello world </span>'
pattern = re.compile(r"(?<=<(\w{4})>)(.*)(?=<\/\1>)")
print(re.findall(pattern,str))#[('span','hello world')]