python學習 Regex,pythonRegex

來源:互聯網
上載者:User

python學習 Regex,pythonRegex

一、re 模組中

1、re.match #從開始位置開始匹配,如果開頭沒有match()就返回none

文法:re.match(pattern, string, flags=0)

pattern 匹配的Regex
string 要匹配的字串。
flags 標誌位,用於控制Regex的匹配方式,如:是否區分大小寫,多行匹配等等。

2、re.search #搜尋整個字串

文法:re.search(pattern, string, flags=0)

pattern 匹配的Regex
string 要匹配的字串。
flags 標誌位,用於控制Regex的匹配方式,如:是否區分大小寫,多行匹配等等。

3、re.findall #搜尋整個字串,返回一個list

文法:re.findall(pattern, string[, flags]):

 

flags:參數

re.I 使匹配對大小寫不敏感
re.L 做本地化識別(locale-aware)匹配
re.M 多行匹配,影響 ^ 和 $
re.S 使 . 匹配包括換行在內的所有字元
re.U 根據Unicode字元集解析字元。這個標誌影響 \w, \W, \b, \B.
re.X 該標誌通過給予你更靈活的格式以便你將Regex寫得更易於理解。

 

# -*- config=utf-8 -*-#Regex#1、處理文本和資料#2、是對字串操作的一種邏輯公式import re;#Python通過re模組提供對Regex的支援。使用re的一般步驟是先將Regex的字串形式編譯為Pattern執行個體,# 然後使用Pattern執行個體處理文本並獲得匹配結果(一個Match執行個體),最後使用Match執行個體獲得資訊,進行其他的操作。pattern=re.compile(r"hello");match=pattern.match("hello Word");if match:    print(match.group());#==================  .匹配任一字元(除了\n)===========================ma=re.match(r"a","a");#只想匹配aprint(ma.group());#返回 a  如果匹配失敗則為空白ma1=re.match(r".","d");print(ma1.group());# 返回 dma2=re.match(r"{.}","{c}");# 匹配大括弧中任一字元(除了\n)print(ma2.group());#=================== [...] 匹配任一字元集===================ma3=re.match(r"{[abc]}","{a}");#匹配大括弧abc中任一字元print(ma3.group());ma4=re.match(r"[a-z]","b");#匹配a-z 小寫任一字元print(ma4.group());ma5=re.match(r"[a-zA-Z]","F");#匹配 a-z 任意a字元print(ma5.group());ma6=re.match(r"[a-zA-Z0-9]","8");#匹配任意字母與數字print(ma6.group());#================== \w 匹配任意單詞字元 =================ma7=re.match(r"[\w]","4");  #print(ma7.group());#=====================\W 任意非單詞字元 =========================ma8=re.match(r"[\W]","*");print(ma8.group());#===================\d 匹配數字===================ma9=re.match(r"[\d]","3"); #等於[0-9]print(ma9.group());#==================== \D 匹配非數字========================ma9=re.match(r"[\D]","(");print(ma9.group());#======================\s 匹配空格=======================ma10=re.match(r"[\s]"," ");print(ma10.group());#======================\S 匹配非空格=======================ma11=re.match(r"[\S]","撒");print(ma11.group());#========================= \ 轉義=========================ma12=re.match(r"\[[\w]\]","[2]");#匹配中括弧中任一字元print(ma12.group())#=============================# -*- config=utf-8 -*-import re;#=================* 匹陪前一個字元 0次或無限次==========ma=re.match(r"[A-Z][a-z]","Aa");print(ma.group());#Aama1=re.match(r"[A-Z][a-z]*","Fdsdasd22");#[a-z]無限多個print(ma1.group());#Fdsdasd#================== + 匹配前一個字元一次或無限次============ma2=re.match(r"[_a-zA-Z]+[_\w]*","_dasd");#匹配 _ 或者字母開頭的任一字元print(ma2.group());#=================== ?匹配前一個字元0次或1次=========================ma3=re.match(r"[1-9]?[0-9]","10");#匹配一個正兩位元包括0print(ma3.group());ma4=re.match(r"[1-9]?[0-9]","08");#print(ma4.group());#0#====================== {m}匹配前一個字元m次 ========================ma5=re.match(r"[0-9]{6}","1313123");#匹配0-9任一字元6次print(ma5.group());#131312#====================== {m,n}匹配前一個字元m-n次 ====================ma6=re.match(r"[a-zA-Z0-9]{3,20}@163.com","jalja365@163.com");#匹配163郵箱print(ma6.group());#=============== *? +? ??儘可能的少匹配===================ma7=re.match(r"[0-9][a-z]*","2we");print(ma7.group());#2wema8=re.match(r"[0-9][a-z]*?","2we");print(ma8.group());#2ma9=re.match(r"[0-9][a-z]+?","2we");print(ma9.group());#2wma10=re.match(r"[0-9][a-z]??","2we");print(ma10.group());#2w#========================================# -*- config=utf-8 -*-import re;#===================== search(pattern,String,flags=0)在一個字串中尋找匹配 ===================str1="jalja_365—1321";ma=re.search(r"\d+",str1);#擷取字串中第一次出現的數字print(ma.group());#365#=================findall(pattern,String,flags=0)返回所有匹配部分的列表===================str2="java=90,python=99,c==300";ma2=re.findall(r"\d+",str2);print(ma2);# ['90', '99', '300'] 擷取所有的數字  以列表的形式返回num=sum([int(x) for x in ma2]);#求列表所有元素的和#==================sub()將字串中匹配正則的字元替換成新的字串=====================str3="java=99";ma3=re.sub(r"\d+","100",str3);print(ma3);#java=100#使用函數def add_1(match):#match 是macth對象即sub()的第一個參數    val=match.group();    print(val);    return str(int(val)+1);ma4=re.sub(r"\d+",add_1,str3);print(ma4);#java=100#==============split()根據匹配規則分割字串返回列表====================str4="jalja:c c++ java Python js,c#";ma5=re.split(r":| |,",str4);print(ma5);#['jalja', 'c', 'c++', 'java', 'Python', 'js', 'c#']

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.