python - re正則匹配模組

來源:互聯網
上載者:User

標籤:國外   group   運算式   sub   class   string   匹配   exe   object   

re模組

re 模組使 Python 語言擁有全部的Regex功能。

compile 函數根據一個模式字串和可選的標誌參數產生一個Regex對象。該對象擁有一系列方法用於Regex匹配和替換。

re 模組也提供了與這些方法功能完全一致的函數,這些函數使用一個模式字串做為它們的第一個參數。

 

re.match函數

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

# (匹配規則,字串,特殊標誌)re.match(pattern, string, flags=0)
re.search方法

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

re.search(pattern, string, flags=0)
re.match與re.search的區別

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

#!/usr/bin/pythonimport re line = "Cats are smarter than dogs"; matchObj = re.match( r‘dogs‘, line, re.M|re.I)if matchObj:   print "match --> matchObj.group() : ", matchObj.group()else:   print "No match!!" matchObj = re.search( r‘dogs‘, line, re.M|re.I)if matchObj:   print "search --> matchObj.group() : ", matchObj.group()else:   print "No match!!"
No match!!search --> matchObj.group() :  dogs
檢索和替換

Python 的 re 模組提供了re.sub用於替換字串中的匹配項。

# pattern 正則中的模式字串。# repl 替換的字串,也可為一個函數。# string 要被尋找替換的原始字串。# count 模式比對後替換的最大次數,預設 0 表示替換所有的匹配。re.sub(pattern, repl, string, count=0, flags=0)
#### 執行個體#!/usr/bin/python# -*- coding: UTF-8 -*- import re phone = "2004-959-559 # 這是一個國外電話號碼" # 刪除字串中的 Python注釋 num = re.sub(r‘#.*$‘, "", phone)print "電話號碼是: ", num # 刪除非數字(-)的字串 num = re.sub(r‘\D‘, "", phone)print "電話號碼是 : ", num
電話號碼是:  2004-959-559 電話號碼是 :  2004959559
 re.compile 函數

compile 函數用於編譯Regex,產生一個Regex( Pattern )對象,供 match() 和 search() 這兩個函數使用。

#### parttern re.compile(pattern[, flags])#### flags # re.I 忽略大小寫# re.L 表示特殊字元集 \w, \W, \b, \B, \s, \S 依賴於當前環境# re.M 多行模式# re.S 即為 . 並且包括分行符號在內的任一字元(. 不包括分行符號)# re.U 表示特殊字元集 \w, \W, \b, \B, \d, \D, \s, \S 依賴於 Unicode 字元屬性資料庫# re.X 為了增加可讀性,忽略空格和 # 後面的注釋re.compile(pattern[, flags])
>>>import re>>> pattern = re.compile(r‘([a-z]+) ([a-z]+)‘, re.I)   # re.I 表示忽略大小寫>>> m = pattern.match(‘Hello World Wide Web‘)>>> print m                               # 匹配成功,返回一個 Match 對象<_sre.SRE_Match object at 0x10bea83e8>>>> m.group(0)                            # 返回匹配成功的整個子串‘Hello World‘>>> m.span(0)                             # 返回匹配成功的整個子串的索引(0, 11)>>> m.group(1)                            # 返回第一個分組匹配成功的子串‘Hello‘>>> m.span(1)                             # 返回第一個分組匹配成功的子串的索引(0, 5)>>> m.group(2)                            # 返回第二個分組匹配成功的子串‘World‘>>> m.span(2)                             # 返回第二個分組匹配成功的子串(6, 11)>>> m.groups()                            # 等價於 (m.group(1), m.group(2), ...)(‘Hello‘, ‘World‘)>>> m.group(3)                            # 不存在第三個分組Traceback (most recent call last):  File "<stdin>", line 1, in <module>IndexError: no such group

 

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.