目录
Python正则表达式匹配特殊字符串匹配特殊的字符串提取特殊的字符串总结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 res = "fullDump_pDevice00000286923A19B0_frame000_1g1s1ch.gfxbench_inst2_F535" # Match the prefix to removeprefix_pattern = r'^fullDump_pDevice\d+_'# Use sub() to remove the matched prefixresult = 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