在一个字符串中搜索匹配正则表达式的第一个位置,返回match对象
>>> import re >>> match = re.search(r'[1-9]\d{5}','BIT 100081') >>> if match: print(match.group(0)) 100081从一个字符串的开始位置起匹配正则表达式,返回match对象
>>> import re >>> match = re.match(r'[1-9]\d{5}','BIT 100081') >>> if match: match.group(0) >>> match.group(0) Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> match.group(0) AttributeError: 'NoneType' object has no attribute 'group' >>> match = re.match(r'[1-9]\d{5}','100081 BIT') >>> if match: match.group(0) '100081'搜索字符串,以列表类型返回全部能匹配的子串
>>> import re >>> ls = re.findall(r'[1-9]\d{5}','BIT100081 TSU100084') >>> ls ['100081', '100084']将一个字符串按照正则表达式匹配结果进行分割,返回列表类型
>>> import re >>> ls = re.findall(r'[1-9]\d{5}','BIT100081 TSU100084') >>> ls ['100081', '100084'] >>> import re >>> re.split(r'[1-9]\d{5}','BIT100081 TSU100084') ['BIT', ' TSU', ''] >>> re.split(r'[1-9]\d{5}','BIT100081 TSU100084',maxsplit=1) ['BIT', ' TSU100084']搜索字符串,返回一个匹配结果的迭代类型,每个迭代元素是match对象
>>> import re >>> for m in re.finditer(r'[1-9]\d{5}','BIT100081 TSU100084'): if m: print(m.group(0)) 100081 100084在一个字符串中替换所有匹配正则表达式的子串,返回替换后的字符串
>>> import re >>> re.sub(r'[1-9]\d{5}',':zipcode','BIT100081 TSU100084') 'BIT:zipcode TSU:zipcode'函数式用法:一次性操作
>>> rst = re.search(r'[1-9]\d{5}','BIT 100081')面向对象用法:编译后的多次操作
>>> import re >>> pat = re.compile(r'[1-9]\d{5}') >>> rest = pat.search('BIT 100081') >>> pat re.compile('[1-9]\\d{5}') >>> rest <re.Match object; span=(4, 10), match='100081'> >>>将正则表达式的字符串形式编译成正则表达式对象
>>> regex = re.compile(r'[1-9]\d{5}')Match对象是一次匹配的结果,包含匹配的很多信息
>>> import re >>> regex = re.compile(r'[1-9]\d{5}') >>> >>> import re >>> match = re.search(r'[1-9]\d{5}','BIT 100081') >>> if match: print(match.group(0)) 100081 >>> type(match)Re库默认采用贪婪匹配,即输出匹配最长的子串
>>> match = re.search(r'PY.*N','PYANBNCNDN') >>> match.group(0) 'PYANBNCNDN'