Python re模組

來源:互聯網
上載者:User

標籤:com   開始   Regex   print   字元   cal   表達   分離   log   

re.match函數

re.match 嘗試從字串的起始位置匹配一個模式,如果不是起始位置匹配成功的話,match()就返回none。

def match(self, string, pos=0, endpos=-1):
"""Matches zero | more characters at the beginning of the string.

import renum = re.compile(r‘(\d{4})-(\d{7})‘)result = num.match(‘my number is 0310-5561921‘)print(result)print(‘#‘*30)num_1 = re.compile(r‘\w*(\d{4})-(\d{7})‘)result = num_1.match(‘mynumberis0310-5561921‘)print(result.groups())print(result.group(1))print(result.group(2))運行:C:\Python27\python.exe D:/Python/modules/Re.pyNone##############################(‘0310‘, ‘5561921‘)03105561921
re.search方法

re.search 掃描整個字串並返回第一個成功的匹配。

num = re.compile(r‘(\d{4})-(\d{7})‘)result_match = num.match(_str)result_search = num.search(_str)print(‘mache : {0}‘.format(result_match))print(‘search : {0}‘.format(result_search.group()))運行:C:\Python27\python.exe D:/Python/modules/Re.pymache : Nonesearch : 0310-5561921
_str = ‘mynumberis0310-5561921‘num_1 = re.compile(r‘\w*(\d{4})-(\d{7})‘)result_match = num_1.match(_str)result_search = num_1.search(_str)# print(‘match.groups:{0}‘.format(result_match.groups()))print(‘match.group(1):{0}‘.format(result_match.group(1)))print(‘match.group(2):{0}‘.format(result_match.group(2)))print(‘search.groups():{0}‘.format(result_search.groups()))print(‘search.group(1):{0}‘.format(result_search.group(1)))print(‘search.group(2):{0}‘.format(result_search.group(2)))運行:match.group(1):0310match.group(2):5561921search.groups():(‘0310‘, ‘5561921‘)search.group(1):0310search.group(2):5561921
re.match與re.search的區別

re.match只匹配字串的開始,如果字串開始不符合Regex,則匹配失敗,函數返回None;而re.search匹配整個字串,直到找到一個匹配。

re.findall函數

找到 re 匹配的所有子串,並把它們作為一個列表返回。這個匹配是從左至右有序地返回。如果無匹配,返回空列

_str = ‘ha1ha222ha3ha4ha5‘num = re.compile(r‘\d‘)print(num.findall(_str))運行:[‘1‘, ‘2‘, ‘2‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘]
re.finditer函數

找到 re 匹配的所有子串,並把它們作為一個迭代器返回。這個匹配是從左至右有序地返回。如果無匹配,返回空列表

_str = ‘ha1ha222ha3ha4ha5‘num = re.compile(r‘\d‘)print(num.finditer(_str))for i in num.finditer(_str):    print(i)    print(i.group())    運行:<callable-iterator object at 0x00000000037C6DD8><_sre.SRE_Match object at 0x00000000037236B0>1<_sre.SRE_Match object at 0x0000000003723718>2<_sre.SRE_Match object at 0x00000000037236B0>2<_sre.SRE_Match object at 0x0000000003723718>2<_sre.SRE_Match object at 0x00000000037236B0>3<_sre.SRE_Match object at 0x0000000003723718>4<_sre.SRE_Match object at 0x00000000037236B0>5
re.split函數

通過Regex將字串分離。如果用括弧將Regex括起來,那麼匹配的字串也會被列入到list中返回。maxsplit是分離的次數,maxsplit=1分離一次,預設為0,不限制次數。

_str = ‘ha1ha222ha3ha4ha5‘result_split = re.split(r‘\d*‘, _str)print(result_split)運行:[‘ha‘, ‘ha‘, ‘ha‘, ‘ha‘, ‘ha‘, ‘‘]

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.