1. 程式人生 > >re模塊詳細介紹

re模塊詳細介紹

ret 表達 數據 精確 tro F12 -s pat ice

\w  匹配字母、數字及下劃線
\W 匹配非字母、數字及下劃線
\s 匹配任意空白字符
\S 匹配任意非空白字符
\d 匹配任意數字,等價於[0-9]
\D 匹配任意非數字
\A 匹配字符串開始
\Z 匹配字符串結束
\n 匹配一個換行符
\t 匹配一個制表符
^ 匹配字符串的開頭
$ 匹配字符串的結尾
. 匹配任意字符,除了換行符,當re.DOTALL標記被指定時,則可以匹配包括換行符的任意字符。
[...] 用來表示一組字符,單獨列出:[amk]匹配‘a‘或‘m‘或‘k‘
[^...]不在[]中的字符:[^amk]匹配除了a,m,k之外的字符
* 匹配0個或多個的表達式
+ 匹配1個或多個的表達式
? 匹配0個或1個由前面的正則表達式定義的片段,非貪婪方式
{n} 精確匹配n個前面表達式
{n,m} 精確匹配n到m次由前面的正則表達式定義的片段,貪婪方式
a|b 匹配a或b
() 匹配括號內的表達式,也表示一個組
.* 默認為貪婪匹配
.*? 為非貪婪匹配:推薦使用
總結:盡量精簡,詳細的如下
盡量使用泛型匹配模式:.*
盡量使用非貪婪模式:.*?
使用括號得到匹配目標:用group(n)去取得結果
有換行符就用re.S修改模式
------------------------------------------------------------------------------------------------------------------
例子:
# coding=utf-8

import re
----- /w /W
ret = re.findall(‘\w‘,‘hello egon 123‘)
print(ret)
ret = re.findall(‘\W‘,‘hello egon 123‘)
print(ret)

------/s /S
ret = re.findall(‘\s‘,‘hello egon 123‘)
print(ret)
ret = re.findall(‘\S‘,‘hello egon 123‘)
print(ret)

-----\d \D
ret = re.findall(‘\d‘,‘hello egon 123‘)
print(ret)
ret = re.findall(‘\D‘,‘hello egon 123‘)
print(ret)

-----\A \Z
ret = re.findall(‘\Ah‘,‘hello egon 123‘)
print(ret)
ret = re.findall(‘123\Z‘,‘hello egon 123‘)
print(ret)

-----\n \t
ret = re.findall(r‘\n‘,‘hello egon \n123‘)
print(ret)
ret= re.findall(r‘\t‘,‘hello egon \t123‘)
print(ret)

----^ $
print(re.findall(‘^h‘,‘hello egon 123‘))
print(re.findall(‘123$‘,‘hello egon 123‘))

---- .
print(re.findall(‘a.b‘,‘alb‘))

----?
print(re.findall(‘ab?‘,‘a‘))
print(re.findall(‘ab?‘,‘abbb‘))

匹配包含小數在內的數字
print(re.findall(‘\d+\.?\d*‘,‘asdfasdf123as1.13dfa12adsf1asdf3‘))
[‘123‘, ‘1.13‘, ‘12‘, ‘1‘, ‘3‘]

---- .* 默認為貪婪匹配
print(re.findall(‘a.*b‘,‘a1b22222222b‘))#[‘a1b22222222b‘]

----- .*?為非貪婪匹配:推薦使用
print(re.findall(‘a(.*?)b‘,‘a1b22222222b‘))#[‘l‘]

---- +
print(re.findall(‘ab+‘,‘a‘))#[]
print(re.findall(‘ab+‘,‘abbbb123bbb‘))#[‘abbbb‘]

---- {n,m}
print(re.findall(‘ab{2}‘,‘abbbb‘))#[‘abb‘]
print(re.findall(‘ab{2,4}‘,‘abbbb‘))#[‘abbbb‘]
print(re.findall(‘ab{1,}‘,‘abbbb‘))#[‘abbbb‘]
print(re.findall(‘ab{2}‘,‘abbbb‘))#[‘abb‘]

----- []
print(re.findall(‘a[l*-]b‘,‘alb a*b a-b‘))#[‘alb‘, ‘a*b‘, ‘a-b‘]#[]內的都為普通字符了,且如果-沒有被轉意的話,應該放到[]的開頭或結尾
print(re.findall(‘a[^1*-]b‘,‘a1b a*b a-b a=b‘))#[‘a=b‘]#[]內的^代表的意思是取反
print(re.findall(‘a[a-z]b‘,‘alb a*b a-b a=b aeb‘))#[‘alb‘, ‘aeb‘]
print(re.findall(‘a[a-zA-Z]b‘,‘a2b a*b a-b a=b aeb aEb‘))#[‘aeb‘, ‘aEb‘]

------- \
print(re.findall(r‘a\\c‘,‘a\c‘))#[‘a\\c‘]

re_str_patt = "\\\\d\\+"
print(re_str_patt) #\\d\+
reObj = re.compile(re_str_patt)
print(reObj.findall("\\d+"))#[‘\\d+‘]

-------- ():分組
print(re.findall(‘(ab)+123‘,‘ababab123‘))#[‘ab‘],匹配到末尾的ab123中的ab
print(re.findall(‘(?:ab)+123‘,‘ababab123‘))#[‘ababab123‘],findall的結果不是匹配的全部內容,而是組內的內容,?:可以讓結果為匹配的全部內容

--------- |
print(re.findall(‘compan(?:y|ies)‘,‘Too many companies have gone bankrupt, and the next one is my company‘))
[‘companies‘, ‘company‘]


------re模塊提供的方法介紹
findall
print(re.findall(‘e‘,‘alex make love‘))#[‘e‘, ‘e‘, ‘e‘]],返回所有滿足匹配條件的結果,放在列表裏
search
print(re.search(‘e‘,‘alex make love‘).group())#e,只到找到第一個匹配然後返回一個包含匹配信息的對象,該對象可以通過調用group()方法得到匹配的字符串,如果字符串沒有匹配,則返回None。
match
print(re.match(‘e‘,‘alex make love‘))#None,同search,不過在字符串開始處進行匹配,完全可以用search+^ 代替match
split
print(re.split(‘[ab]‘,‘abcd‘))#[‘‘, ‘‘, ‘cd‘],先按‘a‘分割得到‘‘和‘bcd‘,再對‘‘和‘bcd‘分別按‘b‘分割
sub
print(re.sub(‘a‘,‘A‘,‘alex make love‘))#Alex mAke love

print(re.sub(‘a‘,‘A‘,‘alex make love‘,1))#Alex make love

print(re.sub(‘a‘,‘A‘,‘alex make love‘,2))#Alex mAke love

print(re.sub(‘^(\w+)(.*?\s)(\w+)(.*?\s)(\w+)(.*?)$‘,r‘\5\2\3\4\1‘,‘alex make love‘))#love make alex

print(re.subn(‘a‘,‘A‘,‘alex make love‘))#(‘Alex mAke love‘, 2)結果帶有總替換個數

compile
obj = re.compile(‘\d{2}‘)
s = ‘abc123eeee‘
print(obj.findall(s))#[‘12‘]
print(obj.search(s).group())#12

--------補充

print(re.findall(‘<(?P<tag_name>\w+)>\w+</(?P=tag_name)>‘,‘<h1>hello</h1>‘))#[‘h1‘]

print(re.search(‘<(?P<tag_name>\w+)>\w+</(?P=tag_name)>‘,‘<h1>hello</h1>‘).group())#<h1>hello</h1>

print(re.search(r‘<(\w+)>\w+</(\w+)>‘,‘<h1>hello</h1>‘).group())#<h1>hello</h1>
print(re.search(r‘<(\w+)>\w+</\1>‘,‘<h1>hello</h1>‘).group())#<h1>hello</h1>

print(re.findall(‘-?\d+\.\d*|(-?\d+)‘,‘1-2*(60+(-40.35/5)-(-4*3))‘))
找出所有整數[‘1‘, ‘-2‘, ‘60‘, ‘‘, ‘5‘, ‘-4‘, ‘3‘]
print(re.findall(‘-?\d+\.?\d*‘,‘1-2*(60+(-40.35/5)-(-4*3))‘))#[‘1‘, ‘-2‘, ‘60‘, ‘-40.35‘, ‘5‘, ‘-4‘, ‘3‘]

expression=‘1-2*((60+2*(-3-40.0/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))‘
print(re.search(‘\(([\+\-\*/]\d+\.?\d*)+\)‘,expression).group())##(-3-40.0/5)

最常規匹配
content=‘Hello 123 456 World_This is a Regex Demo‘
res=re.match(‘Hello\s\d\d\d\s\d{3}\s\w{10}.*Demo‘,content)
print(res)
print(res.group())
print(res.span())

泛匹配
content=‘Hello 123 456 World_This is a Regex Demo‘
res=re.match(‘^Hello.*‘,content)
print(res.group())

匹配目標,獲得指定數據

content=‘Hello 123 456 World_This is a Regex Demo‘
res=re.match(‘^Hello\s(\d+)\s(\d+)\s.*Demo‘,content)
print(res.group()) #取所有匹配的內容
print(res.group(1)) #取匹配的第一個括號內的內容
print(res.group(2)) #去匹配的第二個括號內的內容

貪婪匹配:.*代表匹配盡可能多的字符
content=‘Hello 123 456 World_This is a Regex Demo‘
res = re.match(‘^He.*(\d+).*Demo$‘,content)
print(res.group(1))#6,因為.*會盡可能多的匹配,然後後面跟至少一個數字

非貪婪匹配: ?匹配盡可能少的字符
content=‘Hello 123 456 World_This is a Regex Demo‘
res = re.match(‘^He.*?(\d+).*Demo$‘,content)
print(res.group(1))#123

匹配模式:不能匹配換行符
content=‘‘‘Hello 123 456 World_This
is a Regex Demo
‘‘‘
res = re.match(‘He.*?(\d+).*?Demo$‘,content)
print(res)#None
res = re.match(‘He.*?(\d+).*?Demo$‘,content,re.S)
print(res.group(1))#123

轉義:\
content=‘price is $5.00‘
res=re.match(‘price is $5.00‘,content)
print(res)#None

res=re.match(‘price is \$5\.00‘,content)
print(res.group())#price is $5.00






































re模塊詳細介紹