import re
#使用(?P<name>...) 或者 (?P=name)来标记变量,使用\g<name>的方式来检索
test ='110-120'
result = re.sub(r'1(?P<rest>[0-9]+)','A\g<rest>',test)print(result)#使用\+数字来索引已经标记的分组
test ='aabb-aabb'
results = re.findall(r'([a-z]+)-(\1)',test)print(results)# (?i)忽略大小写
th = re.compile(r'(?i)th')
results = re.finditer(th,'The quickest way is through this tunnel')for result in results:print(result.group())print(result.start())print(result.end())print(result.span())#(?im) 忽略大小写且匹配多行
th = re.compile(r'(?im)th')
results = re.findall(th,'''The quickest
way is through
this tunnel
''')print(results)#(?s)使得.号可以匹配\n
results = re.findall(r'th.*','''The first line
the second line
the third line
''')print(results)
results = re.findall(r'(?s)th.*','''The first line
the second line
the third line
''')print(results)#使用(?m)跨行来查找,第一个例子将找不到第三行的th,因为这里不认为第三行是一个行开头,而是#这三行的一部分,换句话说,默认情况下查找将这三行作为一个整体,而不是每一行作为一个部分#依次去查找
results = re.findall(r'^th','''The first line
another line
the third line
''')print(results)
results = re.findall(r'(?m)^th','''The first line
another line
the third line
''')print(results)#(?x)可以让正则表达式更加易懂,忽略所有的空白字符#想要使用空白字符可以用字符组实现,例如空格[ ]
email = re.compile(r'''(?x)
[\w\.]+
@
[\w\.]+
\.com
''')
result = re.match(email,'cm.bioinformatcis@gmail.com')if result:print(result.group())#re.sub 可以通过指定第四个参数来指定替换的次数,默认所有匹配的位置都会匹配print(re.sub(r'aabb','ccdd','aabb-aabb-aabb'))print(re.sub(r'aabb','ccdd','aabb-aabb-aabb',1))#re.split可以指定多个分隔符,并通过第三个参数来指定分割的次数print(re.split(';|-|\+','a;b-c+d'))print(re.split(';|-|\+','a;b-c+d',1))