Python re函數

來源:互聯網
上載者:User

標籤:函數   python   正則匹配   

一:什麼是正則?   正則就是用一些具有特殊含義的符號組合到一起(稱為Regex)來描述字元或者字串的方法。或者說:正則就是用來描述一類事物的規則。(在Python中)它內嵌在Python中,並通過 re 模組實現。Regex模式被編譯成一系列的位元組碼,然後由用 C 編寫的匹配引擎執行。

二:常用匹配模式(元字元)

650) this.width=650;" src="https://s4.51cto.com/oss/201710/27/9d11c38a16631570d3398b9421884cf1.png-wh_500x0-wm_3-wmp_4-s_4099435571.png" title="1036857-20170529203214461-666088398.png" alt="9d11c38a16631570d3398b9421884cf1.png-wh_" />


# =================================匹配模式=================================

#一對一的匹配

# ‘hello‘.replace(old,new)

# ‘hello‘.find(‘pattern‘)


#正則匹配

import re

#\w與\W

print(re.findall(‘\w‘,‘hello egon 123‘)) #[‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘e‘, ‘g‘, ‘o‘, ‘n‘, ‘1‘, ‘2‘, ‘3‘]

print(re.findall(‘\W‘,‘hello egon 123‘)) #[‘ ‘, ‘ ‘]


#\s與\S

print(re.findall(‘\s‘,‘hello  egon  123‘)) #[‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘]

print(re.findall(‘\S‘,‘hello  egon  123‘)) #[‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘e‘, ‘g‘, ‘o‘, ‘n‘, ‘1‘, ‘2‘, ‘3‘]


#\n \t都是空,都可以被\s匹配

print(re.findall(‘\s‘,‘hello \n egon \t 123‘)) #[‘ ‘, ‘\n‘, ‘ ‘, ‘ ‘, ‘\t‘, ‘ ‘]


#\n與\t

print(re.findall(r‘\n‘,‘hello egon \n123‘)) #[‘\n‘]

print(re.findall(r‘\t‘,‘hello egon\t123‘)) #[‘\n‘]


#\d與\D

print(re.findall(‘\d‘,‘hello egon 123‘)) #[‘1‘, ‘2‘, ‘3‘]

print(re.findall(‘\D‘,‘hello egon 123‘)) #[‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘ ‘, ‘e‘, ‘g‘, ‘o‘, ‘n‘, ‘ ‘]


#\A與\Z

print(re.findall(‘\Ahe‘,‘hello egon 123‘)) #[‘he‘],\A==>^

print(re.findall(‘123\Z‘,‘hello egon 123‘)) #[‘he‘],\Z==>$


#^與$

print(re.findall(‘^h‘,‘hello egon 123‘)) #[‘h‘]

print(re.findall(‘3$‘,‘hello egon 123‘)) #[‘3‘]


# 重複匹配:| . | * | ? | .* | .*? | + | {n,m} |

#.

print(re.findall(‘a.b‘,‘a1b‘)) #[‘a1b‘]

print(re.findall(‘a.b‘,‘a1b a*b a b aaab‘)) #[‘a1b‘, ‘a*b‘, ‘a b‘, ‘aab‘]

print(re.findall(‘a.b‘,‘a\nb‘)) #[]

print(re.findall(‘a.b‘,‘a\nb‘,re.S)) #[‘a\nb‘]

print(re.findall(‘a.b‘,‘a\nb‘,re.DOTALL)) #[‘a\nb‘]同上一條意思一樣


#*

print(re.findall(‘ab*‘,‘bbbbbbb‘)) #[]

print(re.findall(‘ab*‘,‘a‘)) #[‘a‘]

print(re.findall(‘ab*‘,‘abbbb‘)) #[‘abbbb‘]


#?

print(re.findall(‘ab?‘,‘a‘)) #[‘a‘]

print(re.findall(‘ab?‘,‘abbb‘)) #[‘ab‘]

#匹配所有包含小數在內的數字

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‘)) #[‘a1b‘]


#+

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

print(re.findall(‘ab+‘,‘abbb‘)) #[‘abbb‘]


#{n,m}

print(re.findall(‘ab{2}‘,‘abbb‘)) #[‘abb‘]

print(re.findall(‘ab{2,4}‘,‘abbb‘)) #[‘abb‘]

print(re.findall(‘ab{1,}‘,‘abbb‘)) #‘ab{1,}‘ ===> ‘ab+‘

print(re.findall(‘ab{0,}‘,‘abbb‘)) #‘ab{0,}‘ ===> ‘ab*‘


#[]

print(re.findall(‘a[1*-]b‘,‘a1b a*b a-b‘)) #[]內的都為一般字元了,且如果-沒有被轉意的話,應該放到[]的開頭或結尾

print(re.findall(‘a[^1*-]b‘,‘a1b a*b a-b a=b‘)) #[]內的^代表的意思是取反,所以結果為[‘a=b‘]

print(re.findall(‘a[0-9]b‘,‘a1b a*b a-b a=b‘)) #[]內的^代表的意思是取反,所以結果為[‘a=b‘]

print(re.findall(‘a[a-z]b‘,‘a1b a*b a-b a=b aeb‘)) #[]內的^代表的意思是取反,所以結果為[‘a=b‘]

print(re.findall(‘a[a-zA-Z]b‘,‘a1b a*b a-b a=b aeb aEb‘)) #[]內的^代表的意思是取反,所以結果為[‘a=b‘]


#\# print(re.findall(‘a\\c‘,‘a\c‘)) #對於正則來說a\\c確實可以匹配到a\c,但是在python解譯器讀取a\\c時,會發生轉義,然後交給re去執行,所以拋出異常

print(re.findall(r‘a\\c‘,‘a\c‘)) #r代表告訴解譯器使用rawstring,即原生字串,把我們正則內的所有符號都當一般字元處理,不要轉義

print(re.findall(‘a\\\\c‘,‘a\c‘)) #同上面的意思一樣,和上面的結果一樣都是[‘a\\c‘]


#():分組

print(re.findall(‘ab+‘,‘ababab123‘)) #[‘ab‘, ‘ab‘, ‘ab‘]

print(re.findall(‘(ab)+123‘,‘ababab123‘)) #[‘ab‘],匹配到末尾的ab123中的ab

print(re.findall(‘(?:ab)+123‘,‘ababab123‘)) #findall的結果不是匹配的全部內容,而是組內的內容,?:可以讓結果為匹配的全部內容


#|

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


# ===========================re模組提供的方法介紹===========================

import re

#1

print(re.findall(‘e‘,‘alex make love‘) )   #[‘e‘, ‘e‘, ‘e‘],返回所有滿足匹配條件的結果,放在列表裡

#2

print(re.search(‘e‘,‘alex make love‘).group()) #e,只到找到第一個匹配然後返回一個包含匹配資訊的對象,該對象可以通過調用group()方法得到匹配的字串,如果字串沒有匹配,則返回None。


#3

print(re.match(‘e‘,‘alex make love‘))    #None,同search,不過在字串開始處進行匹配,完全可以用search+^代替match


#4

print(re.split(‘[ab]‘,‘abcd‘))     #[‘‘, ‘‘, ‘cd‘],先按‘a‘分割得到‘‘和‘bcd‘,再對‘‘和‘bcd‘分別按‘b‘分割


#5

print(‘===>‘,re.sub(‘a‘,‘A‘,‘alex make love‘)) #===> Alex mAke love,不指定n,預設替換所有

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),結果帶有總共替換的個數



#6

obj=re.compile(‘\d{2}‘)


print(obj.search(‘abc123eeee‘).group()) #12

print(obj.findall(‘abc123eeee‘)) #[‘12‘],重用了obj


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.