python-re模組

來源:互聯網
上載者:User

標籤:www.   優先   port   取消   分割   sub   color   迭代器   模組   

#正則元字元:^ $ . * + ? [] {} () \ | 
#()分組
import reprint(re.search(‘(?P<name>[a-z]+)(?P<age>\d+)‘,"asd123fgh456jkl789").group(‘name‘))
re.search() #懶惰模式 只返回第一個匹配的資料,再調用group()方法得到匹配的字串
re.findall() #貪婪模式,但會所有滿足匹配條件的結果,放到列表中。

re.match() #從開始位置匹配,懶惰模式
re.split() #字串分割為列表
re.sub() #替換
re.subn() #替換
re.finditer() #匹配到的資料放在迭代器對象,適用於大批量資料的匹配,
print(re.split(‘[ |]‘,‘hello iam|matt‘)) #將字串按空格或管道符分割
print(re.split(‘[ab]‘,‘asdabcd‘)) #按a或b分割
print(re.sub(‘a‘,‘A‘,‘aaaaaa‘)) #a替換為A
print(re.sub(‘a‘,‘A‘,‘aaaaaa‘,5)) #a替換為A,替換5次
print(re.subn(‘a‘,‘A‘,‘aaaaaa‘)) #並返回匹配次數
rule = re.compile(‘\d‘) #編譯規則
print(rule.findall("12df34gh5h"))

print(re.findall(‘www\.(baidu|163|sina)\.com‘,‘d3d3d3f45g5gwww.baidu.com2ftg5hth6he‘)) #優先只返回分組中資料
print(re.findall(‘www\.(?:baidu|163|sina)\.com‘,‘d3d3d3f45g5gwww.baidu.com2ftg5hth6he‘)) #取消優先順序,返回所有匹配資料


 

python-re模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.