在sublime中正則匹配中文漢字:\u
即可
在python中匹配中文:[\u4e00-\u9fa5]
,正確與否裂明,待驗(yàn)證
測(cè)試代碼如下:
import re
# re.search返回第一個(gè)match對(duì)象巧还,搜索匹配正則表達(dá)式的第一個(gè)位置
match1 = re.search(r'[1-9]\d{5}', 'BIT 100081')
if match1:
print(match1.group(0))
# 從字符串的第一個(gè)位置開始匹配正則表達(dá)式喷面,返回match對(duì)象
match2 = re.match(r'[1-9]\d{5}', '100081 BIT')
if match2:
print(match2.group(0))
# 搜索字符串,以列表形式返回全部能匹配的子串
ls = re.findall(r'[1-9]\d{5}', '100081 BIT BIT 100084 100085')
if ls:
print(ls)
# 分割字符串,按照正則表達(dá)式的形式,返回列表形式焰坪,去掉匹配的部分
sp = re.split(r'[1-9]\d{5}', '100081 BIT 100078 TSU100084 ')
print(sp)
sp2 = re.split(r'[1-9]\d{5}', '100081 BIT 100078 TSU100084 ', maxsplit=1)
print(sp2)
# 迭代類型:匹配內(nèi)容
for m in re.finditer(r'[1-9]\d{5}', "BIT100081 TsU100084"):
if m:
print(m.group(0))
# 替換string字符串琳彩,用新的字符串repl來替換匹配字符串pattern匹配成功部分
re.sub(r'[1-9]\d{5}', ':zipcode', 'BIT100081 TUS100083')
regex = re.compiler(r'[1-9]\d{5}')
match對(duì)象
當(dāng)匹配結(jié)果是多個(gè)字符串時(shí)墓捻,默認(rèn)匹配最長(zhǎng)的忽洛,即貪婪匹配
最小字符串匹配: