python正則表達式實現自動化編程
一.創(chuàng)建正則表達式
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.創(chuàng)建自定義字符
通過-創(chuàng)建自定義字符,例如[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', '']'''到此這篇關于python正則表達式實現自動化編程的文章就介紹到這了,更多相關python 正則表達式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python3.8 微信發(fā)送服務器監(jiān)控報警消息代碼實現
這篇文章主要介紹了python3.8 微信發(fā)送服務器監(jiān)控報警消息代碼實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11

