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,備用號(hào)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)識(shí)
([\w-]+)\s+ # 認(rèn)證用戶
\[(.*?)\]\s+ # 時(shí)間戳
"(.*?)\s+(.*?)\s+HTTP/(\d\.\d)" # 請(qǐng)求信息
\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)義引號(hào))
- 驗(yàn)證復(fù)雜密碼規(guī)則(大小寫+數(shù)字+特殊符號(hào),8-20位)
- 中英文混合文本分詞處理
- 高效提取海量文本中所有URL
- 轉(zhuǎn)換日期格式(MM/DD/YYYY到Y(jié)YYY-MM-DD)
- 識(shí)別并高亮SQL注入特征
- 多模式匹配優(yōu)化(使用正則字典)
- 解析非標(biāo)準(zhǔn)CSV(含逗號(hào)字段)
- 多層級(jí)日志時(shí)間戳轉(zhuǎn)換
- 構(gòu)建簡易模板引擎
以下是10個(gè)練習(xí)題的完整實(shí)現(xiàn)代碼,每個(gè)解決方案均包含詳細(xì)注釋說明:
題1:提取嵌套JSON字符串中的特定字段(忽略轉(zhuǎn)義引號(hào))
import re
import json
def extract_json_field(json_str, target_field):
"""
提取JSON字符串中指定字段的值(處理轉(zhuǎn)義引號(hào))
: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)義引號(hào)\\",使用json.loads解析字符串值
題2:驗(yàn)證復(fù)雜密碼規(guī)則(大小寫+數(shù)字+特殊符號(hào),8-20位)
import re
def validate_password(password):
"""
驗(yàn)證密碼復(fù)雜度:
- 至少1個(gè)大寫字母
- 至少1個(gè)小寫字母
- 至少1個(gè)數(shù)字
- 至少1個(gè)特殊符號(hào)(!@#$%^&*)
- 長度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)
# 對(duì)中文部分進(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:識(shí)別并高亮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(含逗號(hào)字段)
def parse_nonstandard_csv(csv_str):
"""解析帶逗號(hào)的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:多層級(jí)日志時(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于python select.select模塊通信的實(shí)例講解
下面小編就為大家?guī)硪黄趐ython select.select模塊通信的實(shí)例講解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
基于Pygame中Pygame模塊的大戰(zhàn)外星人實(shí)戰(zhàn)
本文主要介紹了基于Pygame中Pygame模塊的大戰(zhàn)外星人實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
Python常用字符串替換函數(shù)strip、replace及sub用法示例
這篇文章主要介紹了Python常用字符串替換函數(shù)strip、replace及sub用法,結(jié)合實(shí)例形式分析了Python針對(duì)字符串替換的常用函數(shù)strip、replace及sub功能及簡單使用技巧,需要的朋友可以參考下2018-05-05
用TensorFlow實(shí)現(xiàn)lasso回歸和嶺回歸算法的示例
本篇文章主要介紹了用TensorFlow實(shí)現(xiàn)lasso回歸和嶺回歸算法的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
pytorch實(shí)現(xiàn)建立自己的數(shù)據(jù)集(以mnist為例)
今天小編就為大家分享一篇pytorch實(shí)現(xiàn)建立自己的數(shù)據(jù)集(以mnist為例),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python基礎(chǔ)之變量基本用法與進(jìn)階詳解
這篇文章主要介紹了Python基礎(chǔ)之變量基本用法與進(jìn)階,結(jié)合實(shí)例形式詳細(xì)分析了Python變量引用、可變和不可變類型、局部變量和全局變量等相關(guān)原理與使用技巧,需要的朋友可以參考下2020-01-01
python cx_Oracle的基礎(chǔ)使用方法(連接和增刪改查)
這篇文章主要給大家介紹了關(guān)于python cx_Oracle的基礎(chǔ)使用方法,其中包括連接、增刪改查等基本操作,并給大家分享了python 連接Oracle 亂碼問題的解決方法,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11

