最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
當前位置: 首頁 - 科技 - 知識百科 - 正文

python正則分組的應(yīng)用

來源:懂視網(wǎng) 責編:小采 時間:2020-11-27 14:29:51
文檔

python正則分組的應(yīng)用

python正則分組的應(yīng)用: 代碼如下:import retext='V101_renow.Android.2.2.Normal.1.Alpha.apkIMSI=460029353813976&MOBILE=&OLD_VERSION=renow.Android.2.1.Normal.1.Alpha&CHANNELID=3'm = re.search(r\w+_(renow.*)
推薦度:
導讀python正則分組的應(yīng)用: 代碼如下:import retext='V101_renow.Android.2.2.Normal.1.Alpha.apkIMSI=460029353813976&MOBILE=&OLD_VERSION=renow.Android.2.1.Normal.1.Alpha&CHANNELID=3'm = re.search(r\w+_(renow.*)

代碼如下:


import re
text='V101_renow.Android.2.2.Normal.1.Alpha.apk?IMSI=460029353813976&MOBILE=&OLD_VERSION=renow.Android.2.1.Normal.1.Alpha&CHANNELID=3'
m = re.search(r"\w+_(renow.*)\.(apk|vir|ipa)\?IMSI=(\d+)&MOBILE=&OLD_VERSION=(.*)&CHANNELID=(.*)", text)
if m:
print m.group(0), '\n', m.group(1),'\n', m.group(2),'\n', m.group(3),'\n', m.group(4),'\n', m.group(5)
else:
print 'not match'



以下是補充:

組是通過 "(" 和 ")" 元字符來標識的。 "(" 和 ")" 有很多在數(shù)學表達式中相同的意思;它們一起把在它們里面的表達式組成一組。舉個例子,你可以用重復限制符,象 *, +, ?, 和 {m,n},來重復組里的內(nèi)容,比如說(ab)* 將匹配零或更多個重復的 "ab"。

例子:如果不引入括號,增個表達式作為一個組,是group(0)

>>> import re
>>> p=re.compile('\d-\d-\d')
>>> m=p.match('2-3-1')
>>> m.groups()
()
>>> m.group()
'2-3-1'
>>> m.group(1)
Traceback (most recent call last):
File "", line 1, in
IndexError: no such group

如果引入括號,可以將上面的表達式分成3組,如下

>>> p=re.compile('(\d)-(\d)-(\d)')
>>> m=p.match('1-2-3')
>>> m.group()
'1-2-3'
>>> m.group(1)
'1'
>>> m.group(0,2,1)
('2-3-1', '2', '1')

也可以給各個組取名字,例如,給第一個數(shù)組取名叫first


>>> p=re.compile('(?P\d)-(\d)-(\d)')
>>> m=p.match('1-2-3')
>>> m.group(1)
'1'
>>> m.group('first')

'1'

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

python正則分組的應(yīng)用

python正則分組的應(yīng)用: 代碼如下:import retext='V101_renow.Android.2.2.Normal.1.Alpha.apkIMSI=460029353813976&MOBILE=&OLD_VERSION=renow.Android.2.1.Normal.1.Alpha&CHANNELID=3'm = re.search(r\w+_(renow.*)
推薦度:
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top