• 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    输出内容

    6g1s1ch
    4g1s3ch
    1g4s1ch
    1g1s1ch
    ['6g1s1ch', '4g1s3ch', '1g4s1ch', '1g1s1ch']
    DB9909
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    提取特殊的字符串

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    上述正则表达式并不能准确替换掉,输出结果还是原来的字符串: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)
    
    • 1
    • 2
    • 3
    • 4

    输出结果:frame000_1g1s1ch.gfxbench_inst2_F535

  • 相关阅读:
    makefile的基本使用
    【数据结构】单链表OJ题(一)
    PTA 甲级 1057 Stack
    背包问题
    搭建Github图床
    Jenkins笔记(一)
    java计算机毕业设计高考报考指南网站MyBatis+系统+LW文档+源码+调试部署
    ChatGPT Edu版本来啦:支持GPT-4o、自定义GPT、数据分析等
    C++ vector 的模拟实现
    灵魂拷问:TCP 四次挥手,可以变成三次吗?
  • 原文地址:https://blog.csdn.net/wjjontheway/article/details/132626238