一文介紹Python中的正則表達(dá)式用法
1. 正則表達(dá)式基礎(chǔ)
1.1 什么是正則表達(dá)式
正則表達(dá)式是一種用于描述和匹配字符串模式的表達(dá)式。它由一系列字符和特殊字符組成,用于在文本中進(jìn)行搜索和替換操作。
1.2 基本匹配規(guī)則
正則表達(dá)式中的基本匹配規(guī)則包括普通字符的匹配、點(diǎn)號(hào)的匹配任意字符、轉(zhuǎn)義字符的使用等。
import re pattern = r"abc" # 匹配字符串 "abc" string = "xyz abc def" result = re.findall(pattern, string) print(result) # Output: ['abc']
1.3 字符類(lèi)和預(yù)定義字符類(lèi)
字符類(lèi)用于匹配指定范圍內(nèi)的字符,預(yù)定義字符類(lèi)則表示常見(jiàn)的字符組合,如數(shù)字、字母、空白字符等。
import re pattern = r"[0-9]" # 匹配任意數(shù)字字符 string = "abc 123 def" result = re.findall(pattern, string) print(result) # Output: ['1', '2', '3']
1.4 量詞和貪婪匹配
量詞用于指定匹配的次數(shù),如匹配0次或多次、匹配1次或多次等。貪婪匹配是指盡可能多地匹配字符,非貪婪匹配則盡可能少地匹配字符。
import re pattern = r"a+" # 匹配一個(gè)或多個(gè)連續(xù)的字符 "a" string = "aaaabbb" result = re.findall(pattern, string) print(result) # Output: ['aaaa']
1.5 邊界匹配
邊界匹配用于限定匹配的位置,如行的開(kāi)頭、行的結(jié)尾、單詞的邊界等。
import re pattern = r"\bhello\b" # 匹配整個(gè)單詞 "hello" string = "hello world" result = re.findall(pattern, string) print(result) # Output: ['hello']
2. 使用re模塊
2.1 re模塊的導(dǎo)入
在使用Python進(jìn)行正則表達(dá)式操作之前,我們需要先導(dǎo)入re模塊。
import re
2.2 re.match()方法
re.match()方法用于從字符串的開(kāi)頭開(kāi)始匹配模式,如果匹配成功,則返回一個(gè)匹配對(duì)象;否則返回None。
import re
pattern = r"hello"
string = "hello world"
result = re.match(pattern, string)
if result:
print("Match found!")
else:
print("No match")2.3 re.search()方法
re.search()方法用于在字符串中搜索匹配模式,如果找到任意位置的匹配,則返回一個(gè)匹配對(duì)象;否則返回None。
import re
pattern = r"world"
string = "hello world"
result = re.search(pattern, string)
if result:
print("Match found!")
else:
print("No match")2.4 re.findall()方法
re.findall()方法用于在字符串中搜索所有匹配模式的子串,并將它們作為列表返回。
import re pattern = r"\d+" string = "I have 10 apples and 20 oranges." result = re.findall(pattern, string) print(result) # Output: ['10', '20']
2.5 re.sub()方法
re.sub()方法用于在字符串中搜索匹配模式的子串,并將其替換為指定的字符串。
import re pattern = r"apple" string = "I have an apple." result = re.sub(pattern, "banana", string) print(result) # Output: "I have an banana."
3. 正則表達(dá)式的高級(jí)用法
3.1 分組和捕獲
正則表達(dá)式中的分組和捕獲允許我們將匹配的子串提取出來(lái),并在后續(xù)操作中使用。
import re
pattern = r"(\d+)-(\d+)-(\d+)" # 匹配日期格式 "YYYY-MM-DD"
string = "Today is 2023-06-28."
result = re.search(pattern, string)
if result:
year = result.group(1)
month = result.group(2)
day = result.group(3)
print(f"Year: {year}, Month: {month}, Day: {day}")
else:
print("No match")3.2 非貪婪匹配
非貪婪匹配是指盡可能少地匹配字符,可以通過(guò)在量詞后加上"?"來(lái)實(shí)現(xiàn)。
import re pattern = r"a+?" string = "aaaaa" result = re.findall(pattern, string) print(result) # Output: ['a', 'a', 'a', 'a', 'a']
3.3 向前界定和向后界定
向前界定和向后界定用于限定匹配的前后條件,但不包括在匹配結(jié)果中。
import re pattern = r"(?<=@)\w+" # 匹配郵箱地址中的用戶名 string = "john@example.com" result = re.findall(pattern, string) print(result) # Output: ['example']
3.4 反向引用
反向引用用于在正則表達(dá)式中引用前面已經(jīng)匹配的子串。
import re pattern = r"(\w+)\s+\1" # 匹配重復(fù)的單詞 string = "hello hello world world" result = re.findall(pattern, string) print(result) # Output: ['hello', 'world']
3.5 零寬斷言
零寬斷言用于匹配某個(gè)位置前或后的子串,但不包括在匹配結(jié)果中。
import re pattern = r"\d+(?= dollars)" # 匹配 "dollars" 前面的數(shù)字 string = "I have 100 dollars." result = re.findall(pattern, string) print(result) # Output: ['100']
4. 實(shí)例演示
4.1 郵箱驗(yàn)證
使用正則表達(dá)式驗(yàn)證輸入的字符串是否為有效的郵箱地址。
import re
pattern = r"^\w+@\w+\.\w+$" # 匹配郵箱地址
email = "test@example.com"
result = re.match(pattern, email)
if result:
print("Valid email address")
else:
print("Invalid email address")4.2 URL提取
從文本中提取所有的URL鏈接。
import re pattern = r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+" text = "Visit my website at https://example.com. You can also check out https://example.org." result = re.findall(pattern, text) print(result) # Output: ['https://example.com', 'https://example.org']
4.3 HTML標(biāo)簽提取
從HTML文檔中提取所有的標(biāo)簽內(nèi)容。
import re pattern = r"<([^>]+)>" # 匹配HTML標(biāo)簽 html = "<h1>Hello</h1><p>World</p>" result = re.findall(pattern, html) print(result) # Output: ['h1', '/h1', 'p', '/p']
4.4 敏感詞過(guò)濾
使用正則表達(dá)式過(guò)濾文本中的敏感詞。
import re
sensitive_words = ["bad", "evil", "dangerous"]
text = "This is a bad example."
for word in sensitive_words:
pattern = fr"\b{re.escape(word)}\b" # 匹配敏感詞并確保單詞邊界
text = re.sub(pattern, "***", text)
print(text) # Output: "This is a *** example."結(jié)論
本文介紹了Python中正則表達(dá)式的基礎(chǔ)知識(shí)和高級(jí)用法,包括基本匹配規(guī)則、使用re模塊進(jìn)行正則操作的方法以及一些常見(jiàn)的實(shí)例演示。掌握正則表達(dá)式的技巧和應(yīng)用,將能夠更高效地處理和處理文本數(shù)據(jù)。希望本文能夠?qū)δ赑ython中使用正則表達(dá)式有所幫助。
以上就是一文介紹Python中的正則表達(dá)式用法的詳細(xì)內(nèi)容,更多關(guān)于Python正則表達(dá)式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python?pygame項(xiàng)目實(shí)戰(zhàn)英雄動(dòng)畫(huà)特效實(shí)現(xiàn)
這篇文章主要為大家介紹了Python?pygame項(xiàng)目實(shí)戰(zhàn)英雄動(dòng)畫(huà)特效實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
淺談python在提示符下使用open打開(kāi)文件失敗的原因及解決方法
今天小編就為大家分享一篇淺談python在提示符下使用open打開(kāi)文件失敗的原因及解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
python生成不重復(fù)隨機(jī)數(shù)和對(duì)list亂序的解決方法
下面小編就為大家分享一篇python生成不重復(fù)隨機(jī)數(shù)和對(duì)list亂序的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
pandas DataFrame 刪除重復(fù)的行的實(shí)現(xiàn)方法
這篇文章主要介紹了pandas DataFrame 刪除重復(fù)的行的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
python實(shí)現(xiàn)Nao機(jī)器人的單目測(cè)距
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)Nao機(jī)器人的單目測(cè)距,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Python如何計(jì)算兩個(gè)不同類(lèi)型列表的相似度
在編程中,經(jīng)常需要比較兩個(gè)列表的相似度,尤其是當(dāng)這兩個(gè)列表包含不同類(lèi)型的元素時(shí),下面小編就來(lái)講講如何使用Python計(jì)算兩個(gè)不同類(lèi)型列表的相似度吧2025-02-02

