網(wǎng)站首頁 編程語言 正文
1.簡介
# 正則表達(dá)式:用來匹配字符串的武器; # 設(shè)計思想:用一種描述性的語言來給字符串定義一個規(guī)則,凡是符合規(guī)則的字符串,認(rèn)為匹配,否則,該字符串是不合法的; # 實例:判斷一個字符串是否是合法的Email方法: # 1.創(chuàng)建一個匹配Email的正則表達(dá)式; # 2.用該正則表達(dá)式去匹配用戶的輸入來判斷是否合法; # 如:\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"; # 精準(zhǔn)匹配,用[]表示范圍 # 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對象:
----------------------------------------------------
匹配失敗,返回一個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:
match_group(0): 020-6722053
match_group(1): 020
match_group(2): 6722053
---------------------------------------------------------
website_match_test:
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.編譯
# 使用正則表達(dá)式,re模塊內(nèi)部: # a.編譯正則表達(dá)式,如果正則表達(dá)式的字符串本身不合法,拋出錯誤; # b.用編譯后的正則表達(dá)式去匹配字符串; # c.如果一個正則表達(dá)式要重復(fù)使用幾千次,考慮效率, # 可以預(yù)編譯正則表達(dá)式,重復(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é)
原文鏈接:https://blog.csdn.net/qq_39032096/article/details/123491518
相關(guān)推薦
- 2023-05-06 Python執(zhí)行ping操作的簡單方法_python
- 2022-10-26 Google?Kubernetes?Engine?集群實戰(zhàn)詳解_云其它
- 2022-03-06 C語言之快速排序算法(遞歸Hoare版)介紹_C 語言
- 2022-11-25 jar包在linux服務(wù)器已經(jīng)運(yùn)行好但是訪問不到地址的問題及解決方法_Linux
- 2022-10-18 EasyUI使用DataGrid實現(xiàn)動態(tài)列數(shù)據(jù)綁定_jquery
- 2022-06-01 Android實現(xiàn)簡單的照相功能_Android
- 2023-04-01 Python之維度dim的定義及其理解使用方式_python
- 2022-05-11 React中的Refs屬性你來了解嗎_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支