python三十八:re模块
生活随笔
收集整理的這篇文章主要介紹了
python三十八:re模块
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
? 正則表達式(re模塊)是一種小型的,高度專業(yè)化的編程語言。(在python中)它內嵌在python中,并通過re模塊實現(xiàn)。正則表達式模塊被編譯成一系列字節(jié)碼,然后由用c編寫的匹配引擎執(zhí)行。
? 正則就是處理字符串的。
1.普通字符:字符串本身的方法。比如字符串的 find(), split().
?
2. 元字符: ? .^$*+?{}[]|()\
import re s = "hellopythonworldpydsonds" print(re.findall("py..on", s)); # .通配符,可以表示任意字符(\n除外)s = "pythonhelloworldpydsonds" print(re.findall("^py..on", s)); # ^ 被模糊匹配的字符串必須在字符串的開頭位置s = "pythonhelloworlddspydson" print(re.findall("py..on$", s)); # $ 被模糊匹配的字符串必須在字符串的末尾位置s = "pythonheguannnlddspydson" print(re.findall("guan*", s)); # * 匹配0至無窮次的字符, guan*表示匹配 gua, guan, guann, guannn...(n可以是0個, 0到無窮個,貪婪匹配(盡可能匹配))s = "pythonheguannnlddspydson" print(re.findall("guan+", s)); # + 匹配1至無窮次的字符, guan+表示匹配 guan, guann, guannn...(n至少1個, 1到無窮個,貪婪匹配(盡可能匹配))s = "pythonhegualddspydson" print(re.findall("guan*", s)); # * 匹配0至無窮次的字符, guan*表示匹配 gua, guan, guann, guannn...(n可以是0個, 0到無窮個,貪婪匹配(盡可能匹配))s = "pythonhegualddspydson" print(re.findall("guan+", s)); # + 匹配1至無窮次的字符, guan+表示匹配 guan, guann, guannn...(n至少1個, 1到無窮個,貪婪匹配(盡可能匹配))s = "pythonhegualddspydson" print(re.findall("guan?", s)); # ? 匹配0或1個字符, guan?表示只能匹配 gua, guan(只能匹配0個n,或1個n的字符,貪婪匹配(盡可能匹配))# {0,}==*, {1,}==+, {0,1}==?, 用{}可以表示,*,+,?的效果 # 當然{}中可以是任意數(shù)字 {0,6}表示可以匹配0-6個字符, {6}表示只能匹配6個字符 s = "pythonheguannlddspydson" print(re.findall("guan{6}", s)); # {6}表示只能匹配6個字符s = "pythonheguannlddspydson" print(re.findall("guan{0,6}", s)); # {0,6}表示可以匹配0-6個字符s = "pythonheguannnlddspydson" print(re.findall("guan*?", s)); # 加?,實現(xiàn)惰性匹配(只匹配最少的)s = "pythonheguannnlddspydson" print(re.findall("guan+?", s)); # 加?,實現(xiàn)惰性匹配(只匹配最少的)s = "pythonheguannnlddspydson" print(re.findall("guan??", s)); # 加?(第一個?表示元字符,實現(xiàn)模糊匹配,第2個?表示惰性匹配),實現(xiàn)惰性匹配(只匹配最少的) [] 表示字符集, 在字符集里有功能的符號: -(范圍) ^(非) \(轉義)反斜杠后邊跟元字符去除特殊功能,比如\.反斜杠后邊跟普通字符實現(xiàn)特殊功能,比如\d\d 可以匹配任何十進制數(shù),相當于[0-9] \D 可以匹配任何非數(shù)字字符,相當于[^0-9] \s 可以匹配任何空白字符,相當于[\t\n\r\f\v] \S 可以匹配任何非空白字符,相當于[^\t\n\r\f\v] \w 匹配任何字母數(shù)字字符,相當于[a-zA-Z0-9] \W 匹配任何非字母數(shù)字字符,相當于[^a-zA-Z0-9] \b 匹配一個特殊字符邊界,比如空格, &, #等 s = "pythonheguanfnlddspydson" print(re.findall("guan[a-z]", s)); # - [a-z],表示字母a到z之間的所有字母s = "pythonheguan7nnlddspydson" print(re.findall("guan[^a-z]", s)); # ^表示非, [^a-z],表示不是字母a到z之間的其他字符s = "1-90+((888*4)/2-5)" print(re.findall("\d+",s))print(re.findall("\D+",s))s = "hello world python!" print(re.findall("\s+",s))s = "hello world python!" print(re.findall("\S+",s))s = "hello wor%ld PYT*Hon! 43&52" print(re.findall("\w+",s))s = "hello wor%ld PYT*Hon! 43&52&43^87" print(re.findall("\W+",s))s = "hello6world" print(re.findall("hello\.world",s)) # "hello\.world"只能匹配"hello.world" s = "hello I# I$ world LIST" print(re.findall("I\\b", s)) # \b在ascii碼中本身就有特殊含義,如果寫成re.findall("I\b", s),那么最終傳給re模塊的就是轉換過的字符而不是\bs = "hello I\crld LIST" print(re.findall("I\\\\c", s))# python解釋器會轉移一次,變成 "I\\c", 然后re模塊再轉移一次,變成"I\c"?分組 "()"
s = "defabdefabdef" print(re.findall("(def)+", s))s = "liubei22guanyu21zhangfei20zhaoyun" print(re.search("(?P<name>[a-z]+)\d+", s).group()) # search()只匹配第一個符合條件的 print(re.search("(?P<name>[a-z]+)(?P<age>\d+)", s).group("name")) # (?P<名稱>)為分組起名稱 print(re.search("(?P<name>[a-z]+)(?P<age>\d+)", s).group("age")) # (?P<名稱>)為分組起名稱s = "567abcdfd545" print(re.match("\d+", s).group()) # 同search, 不過只在字符串開始處進行匹配print(re.split("[ |]", s)) # 分割 ['567', 'abcdf', 'd54', '5']print(re.sub("\d+","a","12345deffun767ction98")) # 替換 print(re.subn("\d","a","12345deffun767ction98")) # 替換it = re.finditer("\d+","124abd456def789uio") #返回一個迭代器 print(it.__next__().group()) print(it.__next__().group())print(re.findall("www\.(baidu|souhu)\.com", "rtewww.baidu.comrete")) # 優(yōu)先獲取()匹配的內容, ['baidu']print(re.findall("www\.(?:baidu|souhu)\.com", "rtewww.baidu.comrete")) # ?:去掉()的優(yōu)先級?
總結
以上是生活随笔為你收集整理的python三十八:re模块的全部內容,希望文章能夠幫你解決所遇到的問題。