import re
match = re.search(r'[1-9]\d{5}','BIT 100081')
if match:
print(match.group(0))
import re
ls = re.findall(r'[1-9]\d{5}','BIT100081 TSU100084')
print(ls)
import re
ls1 = re.split(r'[1-9]\d{5}','BIT100081 TSU100084')
print(ls1)
ls2 = re.split(r'[1-9]\d{5}','BIT100081 TSU100084',maxsplit = 1)
print(ls2)
import re
for i in re.finditer(r'[1-9]\d{5}','BIT100081 TSU100084'):
if i:
print(i.group(0))
import re
print(re.sub(r'[1-9]\d{5}','zipcode','BIT100081 TSU100084'))
注:在使用sub的替换时,无法替换掉“\n”换行符,需先将“\n”替换为空或者空格,再进行替换
import re
m = re.search(r'[1-9]\d{5}','BIT100081 TSU100084')
print(m.string)
print(m.re)
print(m.pos)
print(m.endpos)
print(m.group(0))
print(m.start())
print(m.end())
print(m.span())
Re库默认采用贪婪匹配,即输出匹配最长的子串。
import re
match = re. search(r'PY.*N','PYANBNCNDN')
print(match.group(0))
import re
match = re. search(r'PY.*?N','PYANBNCNDN')
print(match.group(0))