python正则表达式匹配特殊字符串
匹配特殊的字符串
匹配字符串中特定格式的字符串, 在一串字符串中,先找到特殊规则的substring, 然后再提取相关的位置value
strings = ['result-2023-08-18-6g1s1ch-db9909',
'result-2023-08-18-4g1s3ch-db9909',
'result-2023-08-18-1g4s1ch-db9909',
'result-2023-08-18-1g1s1ch-db9909']
pattern = r'(\d+)([gg])(\d+)([ss])(\d+)([cc][hh])'
results = []
for s in strings:
match = re.search(pattern, s)
if match:
print(match.group())
g = match.group(2) #匹配第2个括号的内容
s = match.group(4) #匹配第4个括号的内容
ch = match.group(6) #匹配第6个括号的内容
string = match.group(1) + g + match.group(3) + s + match.group(5) + ch
results.append(string)
print(results)
db_pattern = r'([dd][bb])(\d+)'
match = re.search(db_pattern, strings[0])
if match:
print(match.group())
db = match.group(1) #匹配第2个括号的内容
number = match.group(2) #匹配第4个括号的内容
db_number = db + number输出内容
6g1s1ch
4g1s3ch
1g4s1ch
1g1s1ch
['6g1s1ch', '4g1s3ch', '1g4s1ch', '1g1s1ch']
db9909
提取特殊的字符串
fulldump_pdevice00000286923a19b0_frame000_1g1s1ch.gfxbench_inst2_f535
pdevice后面可能是一串其他数字和字母,只需要截取从frame001开始的字符串,如:
frame000_1g1s1ch.gfxbench_inst2_f535
import re s = "fulldump_pdevice00000286923a19b0_frame000_1g1s1ch.gfxbench_inst2_f535" # match the prefix to remove prefix_pattern = r'^fulldump_pdevice\d+_' # use sub() to remove the matched prefix result = re.sub(prefix_pattern, '', s) print(result)
上述正则表达式并不能准确替换掉,输出结果还是原来的字符串:
fulldump_pdevice00000286923a19b0_frame000_1g1s1ch.gfxbench_inst2_f535
后使用如下表达式:
s = "fulldump_pdevice0000028fd3b19d0_frame000_1g1s1ch.gfxbench_inst2_f535" prefix_pattern = r'^fulldump_pdevice(\d+)([a-za-z0-9]+)_' new = re.sub(prefix_pattern, "", s) print(new)
输出结果:
frame000_1g1s1ch.gfxbench_inst2_f535
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论