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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值