Python字符串處理與正則表達(dá)式用法實(shí)戰(zhàn)指南
前言
本文深入探討Python字符串處理核心方法與正則表達(dá)式實(shí)戰(zhàn)技巧,涵蓋字符串編碼轉(zhuǎn)換、分割替換、正則表達(dá)式語法精髓,并通過日志解析、數(shù)據(jù)清洗等真實(shí)場景案例展示高階應(yīng)用。最后提供10道階梯式練習(xí)題(附完整答案代碼),助你從基礎(chǔ)到進(jìn)階全面掌握文本處理技能。
一、字符串處理核心三劍客
1.1 編碼轉(zhuǎn)換(encode/decode)
text = "中文文本"
utf8_bytes = text.encode('utf-8') # 轉(zhuǎn)為UTF-8字節(jié)流
gbk_str = utf8_bytes.decode('utf-8').encode('gbk', errors='replace') # 轉(zhuǎn)GBK編碼
print(gbk_str.decode('gbk', errors='ignore')) # 輸出:中文文本
注意:處理多語言文本時(shí)需統(tǒng)一編碼格式,errors參數(shù)可指定處理非法字符策略(replace/ignore)
1.2 智能分割(split)
csv_line = "2023-07-25,192.168.1.1,GET /api/data,200"
# 分割時(shí)處理多余空格
parts = [x.strip() for x in csv_line.split(',', maxsplit=3)]
print(parts) # ['2023-07-25', '192.168.1.1', 'GET /api/data', '200']
進(jìn)階:rsplit()反向分割,partition()三段式分割
1.3 靈活替換(replace)
text = "Python2 is outdated, but Python2 code still exists"
new_text = text.replace("Python2", "Python3", 1) # 僅替換第一個(gè)匹配項(xiàng)
print(new_text) # "Python3 is outdated, but Python2 code still exists"
局限:無法處理模式匹配,需正則表達(dá)式進(jìn)階處理
二、正則表達(dá)式深度解析
2.1 re模塊核心方法
import re
# 預(yù)編譯提升性能(重要?。?
PHONE_PATTERN = re.compile(r'(\+86)?1[3-9]\d{9}')
text = "聯(lián)系方式:+8613812345678,備用號15812345678"
matches = PHONE_PATTERN.findall(text)
print(matches) # ['+86', ''] 分組匹配結(jié)果
2.2 正則語法精要
- 分組捕獲:
(pattern)vs(?:pattern)(非捕獲分組) - 貪婪控制:
.*?非貪婪匹配 vs.*貪婪匹配 - 斷言魔法:
(?=...)正向肯定斷言,(?<!...)逆向否定斷言
三、真實(shí)場景實(shí)戰(zhàn)案例
3.1 日志解析(Apache日志)
log_line = '127.0.0.1 - - [25/Jul/2023:15:30:22 +0800] "GET /index.html HTTP/1.1" 200 2326'
pattern = r'''
^(\d+\.\d+\.\d+\.\d+)\s+ # IP地址
([\w-]+)\s+ # 用戶標(biāo)識
([\w-]+)\s+ # 認(rèn)證用戶
\[(.*?)\]\s+ # 時(shí)間戳
"(.*?)\s+(.*?)\s+HTTP/(\d\.\d)" # 請求信息
\s+(\d{3})\s+ # 狀態(tài)碼
(\d+) # 響應(yīng)大小
'''
match = re.compile(pattern, re.X).search(log_line)
if match:
print(f"IP: {match.group(1)}, 狀態(tài)碼: {match.group(8)}")
3.2 數(shù)據(jù)清洗實(shí)戰(zhàn)
def clean_html(raw):
# 刪除HTML標(biāo)簽但保留內(nèi)容
cleaner = re.compile(r'<[^>]+>| | |\n')
# 合并連續(xù)空格
return re.sub(r'\s{2,}', ' ', cleaner.sub(' ', raw))
dirty_html = "<p>Hello World!</p>
"
print(clean_html(dirty_html)) # "Hello World! "
四、進(jìn)階練習(xí)題與答案
練習(xí)題(10題)
- 提取嵌套JSON字符串中的特定字段(忽略轉(zhuǎn)義引號)
- 驗(yàn)證復(fù)雜密碼規(guī)則(大小寫+數(shù)字+特殊符號,8-20位)
- 中英文混合文本分詞處理
- 高效提取海量文本中所有URL
- 轉(zhuǎn)換日期格式(MM/DD/YYYY到Y(jié)YYY-MM-DD)
- 識別并高亮SQL注入特征
- 多模式匹配優(yōu)化(使用正則字典)
- 解析非標(biāo)準(zhǔn)CSV(含逗號字段)
- 多層級日志時(shí)間戳轉(zhuǎn)換
- 構(gòu)建簡易模板引擎
以下是10個(gè)練習(xí)題的完整實(shí)現(xiàn)代碼,每個(gè)解決方案均包含詳細(xì)注釋說明:
題1:提取嵌套JSON字符串中的特定字段(忽略轉(zhuǎn)義引號)
import re
import json
def extract_json_field(json_str, target_field):
"""
提取JSON字符串中指定字段的值(處理轉(zhuǎn)義引號)
:param json_str: 原始JSON字符串
:param target_field: 要提取的字段名
:return: 匹配值列表
"""
# 匹配字段值:字符串、數(shù)字、布爾、null
pattern = re.compile(
rf'"{target_field}"\s*:\s*("(?:\\"|[^"])*"|-?\d+\.?\d*|true|false|null)',
re.IGNORECASE
)
return [json.loads(m.group(1)) if m.group(1).startswith('"')
else eval(m.group(1))
for m in pattern.finditer(json_str)]
# 測試用例
sample_json = '''
{"user": "Alice", "data": "{\\"info\\": \\"secret\\"}", "age": 25}
'''
print(extract_json_field(sample_json, "data")) # 輸出: ['{"info": "secret"}']
關(guān)鍵點(diǎn):正則處理轉(zhuǎn)義引號\\",使用json.loads解析字符串值
題2:驗(yàn)證復(fù)雜密碼規(guī)則(大小寫+數(shù)字+特殊符號,8-20位)
import re
def validate_password(password):
"""
驗(yàn)證密碼復(fù)雜度:
- 至少1個(gè)大寫字母
- 至少1個(gè)小寫字母
- 至少1個(gè)數(shù)字
- 至少1個(gè)特殊符號(!@#$%^&*)
- 長度8-20
"""
pattern = re.compile(
r'^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!@#$%^&*])[\w!@#$%^&*]{8,20}$'
)
return bool(pattern.fullmatch(password))
# 測試
print(validate_password("Abc123#")) # False(長度不足)
print(validate_password("Abcdefg123!")) # True
技巧:使用正向肯定斷言(?=)確保各類型存在
題3:中英文混合文本分詞
def split_chinese_english(text):
"""
中英文分詞:中文按字分割,英文按單詞分割
"""
return re.findall(r'\b\w+\b|[\u4e00-\u9fa5]', text)
# 測試
sample = "Hello世界!Python 代碼 2023"
print(split_chinese_english(sample)) # ['Hello世界', 'Python', '代碼', '2023'] 由于\w+匹配的是單詞,而Hello世界是 連在一起的并非單詞所以 沒有達(dá)到分詞的效果
def split_chinese_english(text):
"""
中英文分詞:中文按字分割,英文按單詞分割
"""
pattern = r'[a-zA-Z0-9]+|[\u4e00-\u9fa5]'
return re.findall(pattern, text)
# 測試
sample = "Hello世界!Python代碼2023"
print(split_chinese_english(sample)) # ['Hello', '世', '界', 'Python', '代', '碼', '2023']
def split_chinese_english(text):
# 先將英文和數(shù)字部分提取出來
english_num_pattern = re.compile(r'[a-zA-Z0-9]+')
english_num_matches = english_num_pattern.findall(text)
# 去除英文和數(shù)字部分,只保留中文和其他字符
chinese_text = english_num_pattern.sub('', text)
# 對中文部分進(jìn)行分詞
chinese_words = jieba.lcut(chinese_text)
result = []
index = 0
for char in text:
if re.match(r'[a-zA-Z0-9]', char):
if index < len(english_num_matches) and text.startswith(english_num_matches[index], text.index(char)):
result.append(english_num_matches[index])
index += 1
elif char in ''.join(chinese_words):
for word in chinese_words:
if text.startswith(word, text.index(char)):
result.append(word)
break
return result
# 測試
sample = "Hello世界!Python代碼2023"
print(split_chinese_english(sample)) # ['Hello', '世界', '!', 'Python', '代碼', '2023']
原理:\b\w+\b匹配英文單詞,[\u4e00-\u9fa5]匹配單個(gè)漢字
題4:高效提取海量文本中的URL
import re
from urllib.parse import urlparse
# 預(yù)編譯提升性能
URL_PATTERN = re.compile(
r'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+(?:[/?#][^\s"]*)?',
re.IGNORECASE
)
def extract_urls_large_file(file_path):
"""流式讀取大文件提取URL"""
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
yield from (urlparse(url).geturl() for url in URL_PATTERN.findall(line))
# 使用示例
# for url in extract_urls_large_file("bigfile.log"):
# print(url)
優(yōu)化點(diǎn):流式讀取避免內(nèi)存溢出,預(yù)編譯正則提升效率
題5:轉(zhuǎn)換日期格式(MM/DD/YYYY到Y(jié)YYY-MM-DD)
def convert_date_format(text):
"""將MM/DD/YYYY格式轉(zhuǎn)為YYYY-MM-DD"""
return re.sub(
r'\b(0?[1-9]|1[0-2])/(0?[1-9]|[12]\d|3[01])/(\d{4})\b',
lambda m: f"{m.group(3)}-{m.group(1).zfill(2)}-{m.group(2).zfill(2)}",
text
)
# 測試
print(convert_date_format("Date: 7/25/2023, 12/31/2024"))
# 輸出:Date: 2023-07-25, 2024-12-31
注意:使用zfill補(bǔ)零,\b確保完整日期匹配
題6:識別并高亮SQL注入特征
def highlight_sql_injection(text):
"""高亮常見SQL注入關(guān)鍵詞"""
keywords = r'union|select|insert|delete|update|drop|--|\/\*'
return re.sub(
f'(python字符串正則匹配,python字符串正則替換,python字符串正則替換)',
lambda m: f'\033[31m{m.group(1)}\033[0m',
text,
flags=re.IGNORECASE
)
# 測試
sample = "SELECT * FROM users WHERE id=1 UNION SELECT password FROM admins"
print(highlight_sql_injection(sample))
效果:在終端顯示紅色高危關(guān)鍵詞
題7:多模式匹配優(yōu)化(正則字典)
class FastMatcher:
def __init__(self):
self.patterns = {
'email': re.compile(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'),
'phone': re.compile(r'\+?\d{1,3}[-. ]?\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4}'),
'hashtag': re.compile(r'#\w+')
}
def match_all(self, text):
return {k: p.findall(text) for k, p in self.patterns.items()}
# 使用示例
matcher = FastMatcher()
print(matcher.match_all("Contact: test@example.com #info 123-456-7890"))
題8:解析非標(biāo)準(zhǔn)CSV(含逗號字段)
def parse_nonstandard_csv(csv_str):
"""解析帶逗號的CSV字段(使用正則)"""
return re.findall(
r'(?:^|,)(?:"((?:[^"]|"")*)"|([^",]*))',
csv_str
)
# 測試
sample = '1, "Quote, test", "Escaped ""Double Quote"""'
print([tuple(filter(None, m)) for m in parse_nonstandard_csv(sample)])
# 輸出:[('1',), ('Quote, test',), ('Escaped "Double Quote"',)]
題9:多層級日志時(shí)間戳轉(zhuǎn)換
from datetime import datetime
def convert_log_timestamp(log_line):
"""轉(zhuǎn)換[25/Jul/2023:15:30:22 +0800]到ISO格式"""
def repl(m):
dt = datetime.strptime(m.group(1), "%d/%b/%Y:%H:%M:%S %z")
return dt.isoformat()
return re.sub(
r'\[(\d{2}/\w{3}/\d{4}:\d{2}:\d{2}:\d{2} [+\-]\d{4})\]',
repl,
log_line
)
# 測試
log = '[25/Jul/2023:15:30:22 +0800] "GET /" 200'
print(convert_log_timestamp(log))
# 輸出:2023-07-25T15:30:22+08:00 "GET /" 200
題10:構(gòu)建簡易模板引擎
class SimpleTemplate:
def __init__(self, template):
self.pattern = re.compile(r'\{\{(\w+)\}\}')
self.template = template
def render(self, **context):
return self.pattern.sub(
lambda m: str(context.get(m.group(1), '')), self.template
)
# 使用示例
tpl = SimpleTemplate("Hello, {{name}}! Your score: {{score}}")
print(tpl.render(name="Alice", score=95))
# 輸出:Hello, Alice! Your score: 95
實(shí)際使用中需根據(jù)具體需求添加異常處理、性能優(yōu)化等邏輯。建議結(jié)合單元測試驗(yàn)證邊界條件。
五、性能優(yōu)化關(guān)鍵點(diǎn)
- 預(yù)編譯正則:重復(fù)使用模式務(wù)必預(yù)編譯
- 惰性匹配:避免.*導(dǎo)致的回溯爆炸
- 原子分組:
(?>pattern)減少回溯 - Unicode優(yōu)化:
re.ASCII標(biāo)志提升英文處理速度 - 替代方案:簡單文本處理優(yōu)先使用字符串方法
掌握字符串處理與正則表達(dá)式,將使你在數(shù)據(jù)處理、日志分析、Web開發(fā)等領(lǐng)域游刃有余。本文涉及的技術(shù)需要反復(fù)實(shí)踐,建議讀者親自測試每個(gè)示例代碼,并嘗試擴(kuò)展練習(xí)題功能。
到此這篇關(guān)于Python字符串處理與正則表達(dá)式用法的文章就介紹到這了,更多相關(guān)Python字符串處理與正則表達(dá)式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Python和XPath解析動(dòng)態(tài)JSON數(shù)據(jù)的操作指南
JSON動(dòng)態(tài)數(shù)據(jù)在Python中扮演著重要的角色,為開發(fā)者提供了處理實(shí)時(shí)和靈活數(shù)據(jù)的能力,動(dòng)態(tài)JSON數(shù)據(jù)的獲取可能涉及到網(wǎng)絡(luò)請求和API調(diào)用,可以使用Python和XPath來解析動(dòng)態(tài)JSON數(shù)據(jù),接下來小編就給大家介紹一下操作步驟2023-09-09
Python的Django中將文件上傳至七牛云存儲的代碼分享
七牛云存儲可以幫助服務(wù)器轉(zhuǎn)存圖片等數(shù)據(jù),類似于Dropbox等存儲服務(wù),這里就帶給大家Python的Django中將文件上傳至七牛云存儲的代碼分享,需要的朋友可以參考下2016-06-06
Python 讀取.shp文件并生成圖幅編號的實(shí)現(xiàn)思路
這篇文章主要介紹了Python 讀取.shp文件并生成圖幅編號,代碼適用于需要處理和分析地理空間數(shù)據(jù)的場景,如城市規(guī)劃、環(huán)境監(jiān)測或自然資源管理,其中它可以幫助用戶讀取特定區(qū)域的Shapefile文件,確定其地理邊界,需要的朋友可以參考下2024-05-05
Python處理時(shí)間日期坐標(biāo)軸過程詳解
這篇文章主要介紹了Python處理時(shí)間日期坐標(biāo)軸過程詳解,當(dāng)日期數(shù)據(jù)作為圖表的坐標(biāo)軸時(shí)通常需要特殊處理,應(yīng)為日期字符串比較長,容易產(chǎn)生重疊現(xiàn)象,需要的朋友可以參考下2019-06-06
Python實(shí)現(xiàn)接口自動(dòng)化測試的方法詳解
Python接口自動(dòng)化測試是一種高效、可重復(fù)的軟件質(zhì)量驗(yàn)證方法,尤其在現(xiàn)代軟件開發(fā)中,它已經(jīng)成為不可或缺的一部分,本文將深入探討如何使用Python進(jìn)行接口自動(dòng)化測試,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08
Python3.6+selenium2.53.6自動(dòng)化測試_讀取excel文件的方法
這篇文章主要介紹了Python3.6+selenium2.53.6自動(dòng)化測試_讀取excel文件的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09

