網站首頁 編程語言 正文
1.簡介
# 正則表達式:用來匹配字符串的武器; # 設計思想:用一種描述性的語言來給字符串定義一個規則,凡是符合規則的字符串,認為匹配,否則,該字符串是不合法的; # 實例:判斷一個字符串是否是合法的Email方法: # 1.創建一個匹配Email的正則表達式; # 2.用該正則表達式去匹配用戶的輸入來判斷是否合法; # 如:\d可以匹配一個數字,\w可以匹配一個字母或數字; # 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個數字,如:"52"; # b.\s可以匹配一個空格,\s+表示至少有一個空格,如:匹配" "等; # c.\d{3,6}表示3-6個數字,如:"584520"; # 精準匹配,用[]表示范圍 # a.[0-9a-zA-Z\_]表示可以匹配一個數字、字母、下劃線; # b.[0-9a-zA-Z\_]+表示可以匹配至少由一個數字、字母或下劃線組成的字符串,如:"Py20"; # c.[a-zA-Z\_][0-9a-zA-Z\_]*表示匹配由字母或下劃線開頭,后接任意個由一個數字、字母或下劃線組成的字符串; # d.[a-zA-Z\_][0-9a-zA-Z\_]{0,19}限制變量長度為1-20個字符; # e.A|B表示匹配A或B,如:(W|w)illard匹配"Willard"或"willard"; # f.^表示行的開頭,^\d表示必須以數字開頭; # g.$表示行的結束,\d$表示必須以數字結束;
# 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.")
# 結果輸出:
匹配成功,返回一個Match對象:
----------------------------------------------------
匹配失敗,返回一個None:
None
----------------------------------------------------
請輸入測試字符串:Willard584520
It's OK.
2.切分字符串
import re str_input = input("Please input test string:") # 通過空格切分字符串 print(re.split(r"\s+", str_input)) # 結果輸出: # Please input test string:Hello Python. # ['Hello', 'Python.']
import re str_input = input("Please input test string:") print(re.split(r"[\s\,]+", str_input)) # 結果輸出: # 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)) # 結果輸出: # 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))
# 結果輸出:
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.編譯
# 使用正則表達式,re模塊內部: # a.編譯正則表達式,如果正則表達式的字符串本身不合法,拋出錯誤; # b.用編譯后的正則表達式去匹配字符串; # c.如果一個正則表達式要重復使用幾千次,考慮效率, # 可以預編譯正則表達式,重復使用時,不需要編譯這個步驟,直接匹配; 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())
# 結果輸出:
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')?
總結
原文鏈接:https://blog.csdn.net/qq_39032096/article/details/123491518
相關推薦
- 2022-06-01 C語言?深入淺出講解指針的使用_C 語言
- 2022-04-18 Python的類成員變量默認初始值的坑及解決_python
- 2022-02-27 Uncaught (in promise) Error: Redirected when going
- 2022-08-20 Linux中sftp常用命令整理_linux shell
- 2022-03-17 總結C#處理異常的方式_C#教程
- 2022-01-17 Failed to load resource: the server responded with
- 2022-09-21 android實現簡單底部導航欄_Android
- 2022-09-27 C#校驗時間格式的場景分析_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支