1.后向引用
pattern = re.compile(r"(w+)")#['hello', 'go', 'go', 'hello']
# pattern = re.compile(r"(w+)s+")#['hello', 'go', 'go']
# pattern = re.compile(r"(w+)s+1")#['go'] 匹配重复的单词
str = 'hello hello go go come come go go hello hello'
pattern = re.compile(r"(?P<word>w+)s+(?P=word)")
print(re.findall(pattern,str))
2.零宽断言
str = "I'm singing while you're dancing."
pattern = re.compile(r"w+(?=ing)")#(?=exp)匹配exp前面的位置
print(re.findall(pattern,str))#['sing', 'danc']
str = 'reading a book'
pattern = re.compile(r"(?<=re)w+")#(?<=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')]