引言
正則表達式(Regular Expression,簡稱 Regex)是一種富強的文本處理東西,廣泛利用於編程、數據驗證、文本查抄與調換等範疇。它可能幫助我們疾速、正確地處理大年夜量文本數據,進步任務效力。本文將經由過程對50個實戰案例的剖析,帶你深刻懂得正則表達式的奧秘。
正則表達式基本
1. 什麼是正則表達式?
正則表達式是一種用來描述或婚配一系列符合某種句法則矩的字符串的語法。在編程言語中,正則表達式平日用於字符串查抄、調換、驗證跟分割等操縱。
2. 正則表達式的構成
正則表達式由字符跟元字符構成。字符可能是任何可打印或弗成打印的字符,而元字符則存在特定的意思,用於定義更複雜的形式。
3. 正則表達式的履行過程
當利用正則表達式停止婚配時,它會從左到右掃描待婚配的字符串,直到找到第一個符合形式的子串。
實戰案例剖析
案例一:提取字符串中的數字
成績描述:從字符串中提取全部的數字。
代碼示例:
import re
text = "There are 123 apples, 456 oranges, and 789 bananas."
pattern = r'\d+'
matches = re.findall(pattern, text)
print(matches) # 輸出: ['123', '456', '789']
案例二:婚配特定格局的郵箱地點
成績描述:婚配符合特定格局的郵箱地點。
代碼示例:
pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
matches = re.findall(pattern, "myemail@example.com, another.email@domain.co.uk")
print(matches) # 輸出: ['myemail@example.com', 'another.email@domain.co.uk']
案例三:查找包含特定形式的行
成績描述:在一個文本文件中查找包含特定形式的行。
代碼示例:
pattern = r'pattern'
with open('example.txt', 'r') as file:
for line in file:
if re.search(pattern, line):
print(line, end='')
案例四:驗證手機號碼格局
成績描述:驗證手機號碼格局能否正確。
代碼示例:
pattern = r'^1[3-9]\d{9}$'
phone_number = "13800138000"
if re.match(pattern, phone_number):
print("手機號碼格局正確")
else:
print("手機號碼格局錯誤")
案例五:提取網頁標題
成績描述:提取網頁標題。
代碼示例:
import requests
url = "https://www.example.com"
html_content = requests.get(url).text
title = re.search(r'<title>(.*?)</title>', html_content, re.S).group(1)
print("網頁標題:", title)
案例六:調換文本中的特定內容
成績描述:將文本中的特定內容調換為其他內容。
代碼示例:
text = "Hello, my name is Alice. I am 25 years old."
pattern = r'\bAlice\b'
replacement = "Bob"
new_text = re.sub(pattern, replacement, text)
print(new_text)
案例七:分割字符串
成績描述:根據特定形式分割字符串。
代碼示例:
text = "apple,banana,cherry"
pattern = r','
parts = re.split(pattern, text)
print(parts) # 輸出: ['apple', 'banana', 'cherry']
案例八:婚配IP地點
成績描述:婚配IP地點格局。
代碼示例:
pattern = r'\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b'
ip_address = "192.168.1.1"
if re.match(pattern, ip_address):
print("IP地點格局正確")
else:
print("IP地點格局錯誤")
案例九:提取HTML標籤中的屬性值
成績描述:提取HTML標籤中的屬性值。
代碼示例:
html_content = "<a href='https://www.example.com'>鏈接</a>"
pattern = r'<a\s+(?:[^>]*?\s+)?href="([^"]*)">'
match = re.search(pattern, html_content)
if match:
print("鏈接地點:", match.group(1))
案例十:婚配日期格局
成績描述:婚配日期格局。
代碼示例:
pattern = r'\b\d{4}-\d{2}-\d{2}\b'
date = "2021-05-13"
if re.match(pattern, date):
print("日期格局正確")
else:
print("日期格局錯誤")
案例十一:提取URL中的域名
成績描述:提取URL中的域名。
代碼示例:
pattern = r'://([^/]+)'
url = "https://www.example.com"
domain = re.search(pattern, url).group(1)
print("域名:", domain)
案例十二:婚配貨幣金額
成績描述:婚配貨幣金額格局。
代碼示例:
pattern = r'\b\d+\.\d{2}\b'
amount = "100.00"
if re.match(pattern, amount):
print("貨幣金額格局正確")
else:
print("貨幣金額格局錯誤")
案例十三:提取XML標籤中的內容
成績描述:提取XML標籤中的內容。
代碼示例:
xml_content = "<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>"
pattern = r'<body>(.*?)</body>'
content = re.search(pattern, xml_content, re.S).group(1)
print("內容:", content)
案例十四:婚配郵政編碼格局
成績描述:婚配郵政編碼格局。
代碼示例:
pattern = r'\b[1-9]\d{5}\b'
postal_code = "12345"
if re.match(pattern, postal_code):
print("郵政編碼格局正確")
else:
print("郵政編碼格局錯誤")
案例十五:提取CSS款式屬性值
成績描述:提取CSS款式屬性值。
代碼示例:
css_content = "color: red; font-size: 12px;"
pattern = r'(\w+):\s*([^;]+);'
attributes = re.findall(pattern, css_content)
print("屬性:", attributes)
案例十六:婚配時光格局
成績描述:婚配時光格局。
代碼示例:
pattern = r'\b\d{2}:\d{2}:\d{2}\b'
time = "12:30:45"
if re.match(pattern, time):
print("時光格局正確")
else:
print("時光格局錯誤")
案例十七:提取URL參數值
成績描述:提取URL參數值。
代碼示例:
pattern = r'([^&=]+)=([^&]*)'
url = "https://www.example.com?name=John&age=30"
params = re.findall(pattern, url)
print("參數:", params)
案例十八:婚配ISBN格局
成績描述:婚配ISBN格局。
代碼示例:
pattern = r'\b(?:ISBN(?:-13)?:?\ )?(?=\d{9}[\dxX])\d{9}[\dxX]\b'
isbn = "978-3-16-148410-0"
if re.match(pattern, isbn):
print("ISBN格局正確")
else:
print("ISBN格局錯誤")
案例十九:提取JSON中的值
成績描述:提取JSON中的值。
代碼示例:
import json
json_content = '{"name": "Alice", "age": 25}'
data = json.loads(json_content)
pattern = r'"(\w+)":\s*"([^"]*)"'
values = re.findall(pattern, json_content)
print("值:", values)
案例二十:婚配HTML標籤中的class屬性值
成績描述:婚配HTML標籤中的class屬性值。
代碼示例:
html_content = "<div class='container'>內容</div>"
pattern = r'<div\s+class="([^"]*)">'
class_name = re.search(pattern, html_content).group(1)
print("class屬性值:", class_name)
案例二十一:婚配HTML標籤中的id屬性值
成績描述:婚配HTML標籤中的id屬性值。
代碼示例:
html_content = "<div id='main'>內容</div>"
pattern = r'<div\s+id="([^"]*)">'
id_name = re.search(pattern, html_content).group(1)
print("id屬性值:", id_name)
案例二十二:婚配HTML標籤中的style屬性值
成績描述:婚配HTML標籤中的style屬性值。
代碼示例:
html_content = "<div style='color: red;'>內容</div>"
pattern = r'<div\s+style="([^"]*)">'
style_value = re.search(pattern, html_content).group(1)
print("style屬性值:", style_value)
案例二十三:婚配HTML標籤中的title屬性值
成績描述:婚配HTML標籤中的title屬性值。
代碼示例:
html_content = "<a title='鏈接'>鏈接</a>"
pattern = r'<a\s+title="([^"]*)">'
title_value = re.search(pattern, html_content).group(1)
print("title屬性值:", title_value)
案例二十四:婚配HTML標籤中的href屬性值
成績描述:婚配HTML標籤中的href屬性值。
代碼示例:
html_content = "<a href='https://www.example.com'>鏈接</a>"
pattern = r'<a\s+href="([^"]*)">'
href_value = re.search(pattern, html_content).group(1)
print("href屬性值:", href_value)
案例二十五:婚配HTML標籤中的src屬性值
成績描述:婚配HTML標籤中的src屬性值。
代碼示例:
html_content = "<img src='image.jpg'>"
pattern = r'<img\s+src="([^"]*)">'
src_value = re.search(pattern, html_content).group(1)
print("src屬性值:", src_value)
案例二十六:婚配HTML標籤中的alt屬性值
成績描述:婚配HTML標籤中的alt屬性值。
代碼示例:
html_content = "<img alt='圖片'>"
pattern = r'<img\s+alt="([^"]*)">'
alt_value = re.search(pattern, html_content).group(1)
print("alt屬性值:", alt_value)
案例二十七:婚配HTML標籤中的width屬性值
成績描述:婚配HTML標籤中的width屬性值。
代碼示例:
html_content = "<img width='100'>"
pattern = r'<img\s+width="([^"]*)">'
width_value = re.search(pattern, html_content).group(1)
print("width屬性值:", width_value)
案例二十八:婚配HTML標籤中的height屬性值
成績描述:婚配HTML標籤中的height屬性值。
代碼示例:
html_content = "<img height='200'>"
pattern = r'<img\s+height="([^"]*)">'
height_value = re.search(pattern, html_content).group(1)
print("height屬性值:", height_value)
案例二十九:婚配HTML標籤中的data屬性值
成績描述:婚配HTML標籤中的data屬性值。
代碼示例:
html_content = "<div data-id='123'>內容</div>"
pattern = r'<div\s+data-id="([^"]*)">'
data_id = re.search(pattern, html_content).group(1)
print("data屬性值:", data_id)
案例三十:婚配HTML標籤中的class屬性值(包含多個class)
成績描述:婚配HTML標籤中的class屬性值(包含多個class)。
代碼示例:
html_content = "<div class='container main'>內容</div>"
pattern = r'<div\s+class="([^"]*)">'
class_value = re.search(pattern, html_content).group(1)
print("class屬性值:", class_value)
案例三十一:婚配HTML標籤中的id屬性值(包含多個id)
成績描述:婚配HTML標籤中的id屬性值(包含多個id)。
代碼示例:
html_content = "<div id='main id1'>內容</div>"
pattern = r'<div\s+id="([^"]*)">'
id_value = re.search(pattern, html_content).group(1)
print("id屬性值:", id_value)
案例三十二:婚配HTML標籤中的style屬性值(包含多個style)
成績描述:婚配HTML標籤中的style屬性值(包含多個style)。
代碼示例:
html_content = "<div style='color: red; font-size: 12px;'>內容</div>"
pattern = r'<div\s+style="([^"]*)">'
style_value = re.search(pattern, html_content).group(1)
print("style屬性值:", style_value)
案例三十三:婚配HTML標籤中的title屬性值(包含多個title)
成績描述:婚配HTML標籤中的title屬性值(包含多個title)。
代碼示例:
html_content = "<a title='鏈接1' title='鏈接2'>鏈接</a>"
pattern = r'<a\s+title="([^"]*)">'
title_value = re.search(pattern, html_content).group(1)
print("title屬性值:", title_value)
案例三十四:婚配HTML標籤中的href屬性值(包含多個href)
成績描述:婚配HTML標籤中的href屬性值(包含多個href)。
代碼示例:
html_content = "<a href='https://www.example.com' href='https://www.example2.com'>鏈接</a>"
pattern = r'<a\s+href="([^"]*)">'
href_value = re.search(pattern, html_content).group(1)
print("href屬性值:", href_value)
案例三十五:婚配HTML標籤中的src屬性值(包含多個src)
成績描述:婚配HTML標籤中的src屬性值(包含多個src)。
代碼示例:
html_content = "<img src='image.jpg' src='image2.jpg'>"
pattern = r'<img\s+src="([^"]*)">'
src_value = re.search(pattern, html_content).group(1)
print("src屬性值:", src_value)
案例三十六:婚配HTML標籤中的alt屬性值(包含多個alt)
成績描述:婚配HTML標籤中的alt屬性值(包含多個alt)。
代碼示例:
html_content = "<img alt='圖片1' alt='圖片2'>"
pattern = r'<img\s+alt="([^"]*)">'
alt_value = re.search(pattern, html_content).group(1)
print("alt屬性值:", alt_value)
案例三十七:婚配HTML標籤中的width屬性值(包含多個width)
成績描述:婚配HTML標籤中的width屬性值(包含多個width)。
代碼示例:
html_content = "<img width='100' width='200'>"
pattern = r'<img\s+width="([^"]*)">'
width_value = re.search(pattern, html_content).group(1)
print("width屬性值:", width_value)
案例三十八:婚配HTML標籤中的height屬性值(包含多個height)
成績描述:婚配HTML標籤中的height屬性值(包含多個height)。
代碼示例:
html_content = "<img height='200' height='300'>"
pattern = r'<img\s+height="([^"]*)">'
height_value = re.search(pattern, html_content).group(1)
print("height屬性值:", height_value)