Python3的正則表達式詳解
1.簡介
# 正則表達式:用來匹配字符串的武器; # 設(shè)計思想:用一種描述性的語言來給字符串定義一個規(guī)則,凡是符合規(guī)則的字符串,認為匹配,否則,該字符串是不合法的; # 實例:判斷一個字符串是否是合法的Email方法: # 1.創(chuàng)建一個匹配Email的正則表達式; # 2.用該正則表達式去匹配用戶的輸入來判斷是否合法; # 如:\d可以匹配一個數(shù)字,\w可以匹配一個字母或數(shù)字; # a. "00\d"可以匹配"008",但無法匹配"00A"; # b. "\d\d\d"可以匹配"009"; # c. "\w\w\d"可以匹配"py3"; # 如: .匹配任意字符 # a. "py."可以匹配"pyc"、"pyt"等; # 匹配變長的字符: # a.用*表示任意個字符(包括0個); # b.用+表示至少一個字符; # c.用?表示0個或1個字符; # d.用{n}表示n個字符; # e.用{n,m}表示n-m個字符; # 實例:\d{2}\s+\d{3,6} # a.\d{2}表示匹配2個數(shù)字,如:"52"; # b.\s可以匹配一個空格,\s+表示至少有一個空格,如:匹配" "等; # c.\d{3,6}表示3-6個數(shù)字,如:"584520"; # 精準匹配,用[]表示范圍 # a.[0-9a-zA-Z\_]表示可以匹配一個數(shù)字、字母、下劃線; # b.[0-9a-zA-Z\_]+表示可以匹配至少由一個數(shù)字、字母或下劃線組成的字符串,如:"Py20"; # c.[a-zA-Z\_][0-9a-zA-Z\_]*表示匹配由字母或下劃線開頭,后接任意個由一個數(shù)字、字母或下劃線組成的字符串; # d.[a-zA-Z\_][0-9a-zA-Z\_]{0,19}限制變量長度為1-20個字符; # e.A|B表示匹配A或B,如:(W|w)illard匹配"Willard"或"willard"; # f.^表示行的開頭,^\d表示必須以數(shù)字開頭; # g.$表示行的結(jié)束,\d$表示必須以數(shù)字結(jié)束;
# re模塊: import re print("匹配成功,返回一個Match對象:") print(re.match(r"^\d{3}\-\d{3,8}$", "020-6722053")) print("----------------------------------------------------") print("匹配失敗,返回一個None:") print(re.match(r"^\d{3}\-\d{3,8}$", "020 6722053")) print("----------------------------------------------------") user_input = input("請輸入測試字符串:") if re.match(r"^W|w{1-10}", user_input): print("It's OK.") else: print("Failed.")
# 結(jié)果輸出:
匹配成功,返回一個Match對象:
<re.Match object; span=(0, 11), match='020-6722053'>
----------------------------------------------------
匹配失敗,返回一個None:
None
----------------------------------------------------
請輸入測試字符串:Willard584520
It's OK.
2.切分字符串
import re str_input = input("Please input test string:") # 通過空格切分字符串 print(re.split(r"\s+", str_input)) # 結(jié)果輸出: # Please input test string:Hello Python. # ['Hello', 'Python.']
import re str_input = input("Please input test string:") print(re.split(r"[\s\,]+", str_input)) # 結(jié)果輸出: # Please input test string:Hello Willard,welcome to FUXI Technology. # ['Hello', 'Willard', 'welcome', 'to', 'FUXI', 'Technology.']
import re str_input = input("Please input test string:") print(re.split(r"[\s\,\.\;]+", str_input)) # 結(jié)果輸出: # Please input test string:Hello;I am Willard.Welcome to FUXI Technology. # ['Hello', 'I', 'am', 'Willard', 'Welcome', 'to', 'FUXI', 'Technology', '']
3.分組
# ()表示要提取的分組(Group) # ^(\d{3})-(\d{3,8})$分別定義了兩個組 import re match_test = re.match(r"^(\d{3})-(\d{3,8})$","020-6722053") print("match_test:", match_test) print("match_group(0):", match_test.group(0)) print("match_group(1):", match_test.group(1)) print("match_group(2):", match_test.group(2)) print("---------------------------------------------------------") website_match_test = re.match(r"(\w{3}).(\w{5}).(\w{3})", "www.baidu.com") print("website_match_test:", website_match_test) print("website_match_test_group(0):", website_match_test.group(0)) print("website_match_test_group(1):", website_match_test.group(1)) print("website_match_test_group(2):", website_match_test.group(2)) print("website_match_test_group(3):", website_match_test.group(3))
# 結(jié)果輸出:
match_test: <re.Match object; span=(0, 11), match='020-6722053'>
match_group(0): 020-6722053
match_group(1): 020
match_group(2): 6722053
---------------------------------------------------------
website_match_test: <re.Match object; span=(0, 13), match='www.baidu.com'>
website_match_test_group(0): www.baidu.com
website_match_test_group(1): www
website_match_test_group(2): baidu
website_match_test_group(3): com
4.貪婪匹配
# 貪婪匹配:匹配盡可能多的字符; import re string_input = input("Please input string:") print("采用貪婪匹配:") print(re.match(r"^(\d+)(0*)$", string_input).groups()) print("---------------------") print("采用非貪婪匹配:") print(re.match(r"^(\d+?)(0*)$", string_input).groups())
Please input string:1008600 采用貪婪匹配: ('1008600', '') --------------------- 采用非貪婪匹配: ('10086', '00')
5.編譯
# 使用正則表達式,re模塊內(nèi)部: # a.編譯正則表達式,如果正則表達式的字符串本身不合法,拋出錯誤; # b.用編譯后的正則表達式去匹配字符串; # c.如果一個正則表達式要重復(fù)使用幾千次,考慮效率, # 可以預(yù)編譯正則表達式,重復(fù)使用時,不需要編譯這個步驟,直接匹配; import re # 編譯 re_telephone = re.compile(r"^(\d{3})-(\d{3,8})$") # 使用 telephone_input1 = input("Willard,please input your telphone number:") telephone_input2 = input("Chen,Please input your telphone number:") print("match:020-6722053,", re_telephone.match(telephone_input1).groups()) print("match:020-6722066,", re_telephone.match(telephone_input2).groups())
# 結(jié)果輸出:
Willard,please input your telphone number:020-6722053
Chen,Please input your telphone number:020-6722066
match:020-6722053, ('020', '6722053')
match:020-6722066, ('020', '6722066')
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
為什么在函數(shù)中運行的?Python?代碼速度更快?
對于Python解釋器來說,讀取和寫入局部變量比全局變量更容易和更快,因為它們的作用域范圍較小2023-09-09淺談sklearn中predict與predict_proba區(qū)別
這篇文章主要介紹了淺談sklearn中predict與predict_proba區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06使用pandas實現(xiàn)連續(xù)數(shù)據(jù)的離散化處理方式(分箱操作)
今天小編就為大家分享一篇使用pandas實現(xiàn)連續(xù)數(shù)據(jù)的離散化處理方式(分箱操作),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11python自動化測試selenium定位frame及iframe示例
這篇文章主要為大家介紹了python自動化測試selenium定位frame及iframe示例的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11Python實現(xiàn)string字符串連接的方法總結(jié)【8種方式】
這篇文章主要介紹了Python實現(xiàn)string字符串連接的方法,結(jié)合實例形式總結(jié)分析了Python實現(xiàn)字符串連接的8種常見操作技巧,需要的朋友可以參考下2018-07-07