網站首頁 編程語言 正文
一.創建正則表達式
1.re模塊
?所有python的正則表達式都在re模塊中,使用時導入
import re
re模塊的compile()方法是構成正則表達式的方法,向compile()傳入一個字符串表示正則表達式,該方法返回一個Regex模式的對象。
2.匹配Regex對象
Regex對象的seach()方法可以查找插入的字符串,如個查找不到匹配的返回null,否則將匹配的值返回到一個Math對象,通過Math對象的group()方法查看匹配結果
import re
ageRegex=re.compile(r'\d\d\d\d')
a=ageRegex.search("今年是2023年")
print(a)
print(a.group())
'''<re.Match object; span=(3, 7), match='2023'>
2023
'''
二.正則表達式匹配更多模式
1.用括號分組
在compile插入字符串是利用括號可以將匹配的數據進行分組,并通過Math對象的group(index)加下標的方式分布顯示,如果查看全部數據用groups()方法
import re
Regex=re.compile(r'(\d\d\d\d)-(\d)-(\d)')
a=Regex.search('今年是2023-1-1')
print(a)
print(a.group(1))
print(a.groups())
'''<re.Match object; span=(3, 11), match='2023-1-1'>
2023
('2023', '1', '1')
'''
2.用管道匹配多個分組
符號|為管道,匹配多個字符串是可以用|連接,例如A|B,就可以匹配A或B,如果A,B都出現在字符串中只返回第一到Math對象中
import re
Regex=re.compile(r'hello|hi')
a=Regex.search('hello world hi time')
print(a.group())
'''hello'''
也可以通過括號將多個匹配的數據分組,group查看匹配的數據,group(1)查看從管道匹配的數據
import re
Regex=re.compile(r'Bat(man|mobile|bat)')
a=Regex.search('Batmobile lost a wheel , l like Batman')
print(a.group())
print(a.group(1))
'''Batmobile
mobile'''
3.用問號表示可選
如果要匹配的數據可以在也可以不在,可以用?來實現匹配
import re
Regex=re.compile(r'hello (world)?')
a=Regex.search('hello world hello time')
b=Regex.search('hello time')
print(a.group())
print(b.group())
'''hello world
hello '''
但字符串中有可選的數據時匹配,如果沒有也不影響其他數據的匹配
4.用星號匹配零次或多次
星號*表示出現零次或多次,即標星號的內容可以出現一次或多次
import re
Regex=re.compile(r'(super)*man')
a=Regex.search('I am superman')
b=Regex.search('I am supersupersuperman')
print(a.group())
print(b.group())
'''superman
supersupersuperman
'''
5.用加號表示匹配一次或多次
星號表示匹配零次或多次,而加號表示匹配一次或多次,加號必須有一次匹配才可以,否則返回的Math對象為None
import re
Regex=re.compile(r'(super)+man')
c=Regex.search('I am man')
a=Regex.search('I am superman')
b=Regex.search('I am supersupersuperman')
print(a.group())
print(b.group())
print(c)
'''superman
supersupersuperman
None
'''
6.用花括號匹配特定次數
(a){3}表示匹配字符串‘aaa’但不匹配‘aa’,如果花括號里有兩個數表示范圍從最小值到最大值,例如(a){3,5}表示匹配字符串‘aaa’,‘aaaa’,‘aaaaa’,也可以省略最大值或最小值
import re
Regex=re.compile(r'(hello){2}')
a=Regex.search('hellohello time hello')
print(a.group())
'''hellohello'''
三.貪心和非貪心匹配
python的正則表達式在默認情況下是貪心的,即在有二意的情況下,默認匹配最多的的字符串,如果想人正則表達式不貪心,可以在花括號后面加上一個問號來約束貪心
import re
Regex=re.compile(r'(a){2,8}?')
Regex1=re.compile(r'(a){2,8}')
string='aaaaaaaaaaaaaa'
a=Regex.search(string)
b=Regex1.search(string)
print(a.group()) #非貪心
print(b.group())
'''aa
aaaaaaaa'''
findall()方法
search()方法將第一次匹配的字符串返回到一個Math對象,findall()和search()方法類似,但findall()方法可以匹配多個匹配的字符串,如果只匹配一個就返回匹配的字符串,如果匹配多個,將返回一個元組,元組里包含匹配的字符串
import re
Regex=re.compile(r'hello')
string='hello world! hello time!'
a=Regex.search(string)
b=Regex.findall(string)
print(a.group())
print(b)
'''hello
['hello', 'hello']'''
四.字符分類
縮寫字符串分類 | 表示 |
\d | 0到9的任何數字 |
\D | 除0到9以外的任意字符 |
\w | 字母,數字或下劃線字符 |
\W | 除字母,數字和下劃線外的任意字符 |
\s | 空格,制表符或換行符 |
\S | 除空格,制表符和換行符外的任意字符 |
例如:\d+匹配一次或多次數字,\s匹配空格,制表符或換行符,\w+匹配字母,數字或下劃線
import re
Regex=re.compile(r'\d+\s\w+')
a=Regex.findall('1 dog,2 pig,3 duck,4 cat,5 fish,6 col')
print(a)
'''['1 dog', '2 pig', '3 duck', '4 cat', '5 fish', '6 col']'''
五.自定義字符分類
1.創建自定義字符
通過-創建自定義字符,例如[a-z]表示匹配a~z的小寫字母,[A-Z0-9]匹配A~Z或0~9,當加上(^)時表示不匹配這些字符串,例如[^a-z]表示不匹配a~z,[aiuoe]表示匹配指定的字母aioue
import re
Regex=re.compile(r'[a-z]')
Regex1=re.compile(r'[aioue]') #元音
string='abcdefghijklmnopqrstuvwsyz'
a=Regex.findall(string)
b=Regex1.findall(string)
print(b)
'''['a', 'e', 'i', 'o', 'u']'''
print(a)
'''['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 's', 'y', 'z']'''
2.插入符號和美元符號
插入符號用(^)表示,插入符號表示匹配的字符串開始的位置,美元符號$表示結束,例如^\d表示從0~9的數字開始匹配,\d$表示匹配0~9結束的字符串
import re
Regex=re.compile(r'^\d+\w+$')
a=Regex.findall('1b32c23d')
print(a)
'''['1b32c23d']'''
3.通配字符
用.表示通配符,通配符可以匹配除了換行之外的所有字符
import re
Regex=re.compile(r'.a.')
a=Regex.findall('dfweascareaefefwa')
print(a)
'''['eas', 'car', 'eae']'''
4.用(.*)匹配所有字符
(.*)匹配所有字符,.表示匹配除換行符的所有字符,*表示出現零次或多次
import re
Regex=re.compile(r'<.*>')
a=Regex.findall('<adf>,<fedf>,<gre>,<fww2>')
print(a)
'''['<adf>,<fedf>,<gre>,<fww2>']'''
5.用參數re.DOTALL匹配換行
正則表達式中通過插入re.DOTALL作為compile的第二參數來人通配符匹配所有字符,包括換行
import re
Regex=re.compile(r'.*',re.DOTALL)
a=Regex.findall('hello world hello time')
print(a)
'''['hello world hello time', '']'''
原文鏈接:https://blog.csdn.net/weixin_63009369/article/details/128513465
相關推薦
- 2022-09-27 Kotlin示例講解標準函數with與run和apply的使用_Android
- 2022-06-16 python讀取txt數據的操作步驟_python
- 2023-03-21 C#實體類轉換的兩種方式小結_C#教程
- 2023-04-17 Python魔術方法深入分析講解_python
- 2022-09-13 Python?迭代器介紹及作用詳情_python
- 2022-04-08 關于Kotlin委托你必須重視的幾個點_Android
- 2022-08-02 Python?Pandas處理CSV文件的常用技巧分享_python
- 2022-11-17 一文詳解測試Python讀寫xml配置文件_python
- 最近更新
-
- 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同步修改后的遠程分支