一、常用方法:
二、實例
import re
repx = re.compile(r'((25[0-5]|2[0-4]\d|1\d{2}|\d?\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|\d?\d)')
match = repx.search('A的ip 255.243.198.1')
print(match.group(0))
match1 = re.match(r'[1-9]\d{5}', '100081SKH')#要注意match是字符串的開始位置匹配 若開始位置沒有就匹配不到
if match1:
print(match1.group(0))
else:
print('Match Filed')
list1 = repx.findall('A的ip 198.254.18.123 B的ip 253.222.111.96' )
for i in list1:
print(i)
for i in repx.finditer('A的ip 198.254.18.123 B的ip 253.222.111.96'):
#每個i都是一個match對象
print(i.group(0))
str = re.sub(r'[1-9]\d{5}', 'RUSHSKH', 'BIOT100081')#返回字符串
print(str)
'''
貪婪匹配和最小匹配
python的re默認爲貪婪匹配 即匹配最大長度
'''
match2 = re.search(r'PY.*N', 'PYDSDSNDSDNDSDSDSAN')#貪婪匹配
print(match2.group(0))#PYDSDSNDSDNDSDSDSAN
match3 = re.search(r'PY.*?N', 'PYDSDSNDSDNDSDSDSAN')
print(match3.group(0))#PYDSDSN