python-正則表達式
阿新 • • 發佈:2017-06-24
模式 不改變 字符串 ont aid 返回 集合 匹配字符串 內容
使用正則表達式時,需要導入包,import re ,簡單使用如下:
匹配字符串的幾個方法
match :從第一個單詞開始匹配,若匹配成功,則返回一個對象;若沒有匹配數據,則返回None
import re s = ‘besttest is gobeod be ‘ # match方法接收3個參數,第一個參數是匹配規則,也就是你要匹配的內容,第二個參數是要查找的字符串,第三個參數為非必填項 #match匹配的模式:從第一個單詞開始匹配,如果匹配到了則返回一個對象,後面滿足規則的匹配不到 輸出>>>be print(re.match(r‘be‘, s).group()) s1= ‘besttest te is bat‘ #match 若從第一個單詞開始匹配,沒有匹配到,則返回None 輸出>>>None print(re.match(r‘te‘, s1))
search: 從整個查找的字符串中進行匹配,若有多個數據滿足匹配規則,則取第一個數據
import re s = ‘besttest is gobeod ‘ # seach方法接收3個參數,第一個參數是匹配規則,也就是你要匹配的內容,第二個參數是要查找的字符串,第三個參數為非必填項 # seach匹配的模式:從字符串的整個內容中查找進行匹配,若匹配成功則返回一個對象;若未匹配成功,則返回None,輸出>>stprint(re.search(r‘st‘, s).group()) >>>輸出st s1 = ‘besttest te is bat‘ #seach 從字符串的整個內容中查找進行匹配,若有多個數據滿足匹配規則,則返回第一個數據,輸出>>te print(re.search(r‘te‘, s1).group()) >>>輸出te print(re.search(r‘aa‘, s1)) >>>輸出None
findall: 從整個查找的字符串中進行匹配,若有多個數據滿足匹配規則,則全部取出,返回list
import re s = ‘besttest is gobeod be‘ # findall方法接收3個參數,第一個參數是匹配規則,也就是你要匹配的內容,第二個參數是要查找的字符串,第三個參數為非必填項 # findall匹配的模式:從字符串的整個內容中查找進行匹配,若匹配成功則返回一個list print(re.findall(r‘be‘, s)) >>>輸出[‘be‘, ‘be‘, ‘be‘] s1 = ‘besttest te is bat‘ #findall從字符串的整個內容中查找進行匹配,若沒有匹配到數據,則返回空list[] print(re.findall(r‘aa‘, s1)) >>>輸出[]
sub:從查找的字符串中進行匹配,若匹配成功,則進行替換,返回一個新值
import re s = ‘besttest is gobeod be‘ #從查找的字符串中找到匹配的值,然後進行替換,返回一個新值,不改變原來的值;若匹配不到,則返回以前的值 new_s = re.sub(r‘best‘, ‘BEST‘, s) print(new_s) >>>輸出BESTtest is gobeod be print(s) >>>輸出besttest is gobeod be
spilt:從查找的字符串中進行匹配,若匹配成功,則以匹配的字符串進行分割,返回結果為list
import re s = ‘besttest is gobeod be is ‘ #從查找的字符串中進行匹配,若匹配成功,則以is進行分割,返回結果為list print(re.split(r‘is‘, s)) >>>輸出[‘besttest ‘, ‘ gobeod be ‘, ‘ ‘] #若從查找的字符串中沒有找到匹配的字符,則將整個字符串作為元素,返回list print(re.split(r‘cc‘, s)) >>>輸出[‘besttest is gobeod be is ‘]
匹配符* :*號匹配前面的字符0次或多次,只匹配*前面的一個字符,即e出現的次數可以為0次或者多次,輸出結果為list
import re s = ‘besttest is gobeod b bef bed e ‘ # 單獨的 e字符是匹配失敗的,*號匹配e的出現次數,但b字符必須存在 print(re.findall(r‘be*‘, s)) >>>輸出[‘be‘, ‘be‘, ‘b‘, ‘be‘, ‘be‘]
匹配符+ :+號匹配前面的字符1次或多次,只匹配+前面的一個字符,即t出現的次數最少為1次,輸出結果為list
import re s = ‘besttest is gobeod s best str t ‘ # 單獨的t字符匹配是失敗的,單獨的s字符匹配也是失敗的,t出現的次數最少為1次 print(re.findall(r‘st+‘, s)) >>>輸出[‘stt‘, ‘st‘, ‘st‘, ‘st‘]
匹配符? :?號匹配前面的字符0次或1次,只匹配?前面的一個字符,即t出現的次數可以為0次也可以為1次,輸出結果為list
import re s = ‘besttest is gobeod s best str t ‘ # 匹配符?,匹配前一個字符出現的次數為0次或者1次,單獨的t字符匹配失敗,單獨的s字符可以匹配成功;若都沒有匹配的數據,則返回空list print(re.findall(r‘st?‘, s)) >>>>輸出[‘st‘, ‘st‘, ‘s‘, ‘s‘, ‘st‘, ‘st‘]
匹配符{n} :{}號匹配字符串出現的次數,n代表出現的次數
import re s = ‘besttest is gobeod s letter better best tt ‘ # 匹配 t出現2次的字符,單獨的tt字符匹配失敗 print(re.findall(r‘t{2}e‘, s)) >>>輸出[‘tte‘, ‘tte‘, ‘tte‘]
匹配符{n, m}:{}號匹配字符串出的n-m次
import re s = ‘besttest is gobeod s letter better tttte te ttttte t ‘ # 匹配 t出現1-4次的字符,可以出現1次,也可以最多出現4次,單獨的t字符匹配失敗 print(re.findall(r‘t{1,4}e‘, s)) >>>輸出[‘tte‘, ‘tte‘, ‘tte‘, ‘tttte‘, ‘te‘, ‘tttte‘]
匹配符. 匹配任意字符
import re s = ‘besttest is gobcod s letter bftter tttte te ttttte t ‘ #默認匹配\n之外的任意字符 print(re.findall(r‘b.‘, s)) >>>輸出[‘be‘, ‘bc‘, ‘bf‘]
匹配符[]:[abcd]匹配中括號內的任意字符即可
import re s = ‘besttest is bej best bec bed bejg ‘ #匹配字符集合,匹配中括號內的任意一個字符即可,返回結果為list print(re.findall(r‘be[stcj]‘, s)) >>>輸出[‘bes‘, ‘bej‘, ‘bes‘, ‘bec‘, ‘bej‘]
匹配符[^]:[^abcd]匹配不滿足中括號內的任意字符,^取反符
import re s = ‘besttest is bej best bec bed bejg ‘ #匹配符[^]取反,也就是匹配不到中括號內的任意字符 print(re.findall(r‘be[^stcj]‘, s)) >>>輸出[‘bed‘]
匹配數字\d,匹配0-9數字
import re s = ‘123asd111233‘ #匹配數字0-9,返回結果list print(re.findall(r‘\d‘, s)) >>>輸出[‘1‘, ‘2‘, ‘3‘, ‘1‘, ‘1‘, ‘1‘, ‘2‘, ‘3‘, ‘3‘]
匹配非數字\D,主要匹配大小寫字母、中文、特殊字符
import re s = ‘123asdA@#&你好111233‘ #匹配非數字,匹配大小寫字母、中文、特殊字符,返回結果list print(re.findall(r‘\D‘, s)) >>>>輸出[‘a‘, ‘s‘, ‘d‘, ‘A‘, ‘@‘, ‘#‘, ‘&‘, ‘你‘, ‘好‘]
匹配符\w,匹配【a-zA-Z0-9】,匹配所有的字母和數字
import re s = ‘123asdAcf@#&111233‘ #匹配[a-zA-Z0-9],匹配所有的字母和數字,返回list print(re.findall(r‘\w‘, s)) >>>輸出[‘1‘, ‘2‘, ‘3‘, ‘a‘, ‘s‘, ‘d‘, ‘A‘, ‘c‘, ‘f‘, ‘1‘, ‘1‘, ‘1‘, ‘2‘, ‘3‘, ‘3‘]
匹配符\W,匹配非【a-zA-Z0-9】,也就是匹配非字母和數字,匹配特殊字符和空格
import re s = ‘123asdAcf@ # &你好111233‘ #匹配[a-zA-Z0-9],匹配所有的字母和數字,返回list print(re.findall(r‘\W‘, s)) >>>輸出[‘@‘, ‘ ‘, ‘#‘, ‘ ‘, ‘&‘]
匹配符\s,匹配空白符\t\n\r空格等
import re s = ‘axss\n\tsdf\t\r\t‘ #匹配\t\n\t空格 print(re.findall(r‘\s‘, s)) >>>輸出[‘\n‘, ‘\t‘, ‘\t‘, ‘\r‘, ‘\t‘]
匹配符\S,匹配非空白字符,非\t\n\r空格等
import re s = ‘axss\n\tsdf\t\r\t‘ #匹配非\t\n\t空格 print(re.findall(r‘\S‘, s)) >>>輸出[‘a‘, ‘x‘, ‘s‘, ‘s‘, ‘s‘, ‘d‘, ‘f‘]
匹配手機號(包含13|15|18系列)
import re #匹配手機號 phone = ‘13462245029,12345678901,134000000002,1234‘ #手機號以1開頭,[358]匹配第二位358中任意一位即可,後面9位取[0-9]{9}取9位,如果手機號有長度為12位的,則取前11位 print(re.findall(r‘1[358]\d{9}‘, phone))
匹配郵箱,email地址
import re mail = ‘[email protected],@qq.com,[email protected],abd#163.com,[email protected]‘ #[email protected],@符前後得有[a-zA-Z0-9]必須出現一次,後綴可以是.com|.cn|.com.cn print(re.findall(r‘(\w+@\w+(\.com\.cn|\.com|\.cn))‘, mail)) >>>輸出[(‘[email protected]‘, ‘.com‘), (‘[email protected]‘, ‘.com.cn‘)]
匹配url
import re url = ‘http://www.baidu.com,https://autoho.cn,htpp://sougou.com.cn‘ print(re.findall(r‘http:\/\/[^\s]*‘, url)) >>>輸出[‘http://www.baidu.com,https://autoho.cn,htpp://sougou.com.cn‘]
python-正則表達式