代码之家  ›  专栏  ›  技术社区  ›  Rilcon42

获取字符串中由未知分隔符分隔的最后一个数字

  •  -1
  • Rilcon42  · 技术社区  · 6 年前

    我试图解析标题,但我不太明白如何隔离字符串中的最后一个数字,我想我找到了分隔符(使用那些嵌套的if语句),但我仍然无法通过自己的测试用例。有什么建议吗?

    电流输出:

    1801 (150@$5): 1801
    0055 30 @ $5: 0055
    leaver - 8 @ $10: 8
    ATS-55) - 45/$2: 55
    

    最终目标:

    1801 (150@$5): 150
    0055 30 @ $5: 30
    leaver - 8 @ $10: 8
    ATS-55) - 45/$2: 45
    

    我的代码

    import re
    
    def getSlots(title):
        x=title.split('@')
        if len(x)<2: #this means @ wasnt used as the delimeter
            x=title.split('/')
            if len(x)<2:
                x=title.split(' ')
                if len(x)<2:
                    return "unsolvable";
    
        m = re.search('\d+', x[0])
        return m.group(0);
    
    testlist=['1801 (150@$5)','0055 30 @ $5','leaver - 8 @ $10','ATS-55) - 45/$2']
    for t in testlist:
        print(t+': '+getSlots(t))
    
    3 回复  |  直到 6 年前
        1
  •  0
  •   mhawke    6 年前

    使用正则表达式并假设所需的数字字符串后跟一个或多个空格, @ / 人物与决赛 $ :

    import re
    
    testlist = ['1801 (150@$5)', '0055 30 @ $5', 'leaver - 8 @ $10', 'ATS-55) - 45/$2']
    
    for s in testlist:
        match = re.search(r'(\d+)[ @/]+\$', s)
        if match:
            print('{}: {}'.format(s, match.groups()[0]))
    

    产量

    1801 (150@$5): 150
    0055 30 @ $5: 30
    leaver - 8 @ $10: 8
    ATS-55) - 45/$2: 45
    
        2
  •  0
  •   jedwards    6 年前

    假设您要查找的数字始终是美元符号左侧的连续连续数字集,则如下所示似乎有效:

    lines = [
        '1801 (150@$5): 1801',
        '0055 30 @ $5: 0055',
        'leaver - 8 @ $10: 8',
        'ATS-55) - 45/$2: 55',
    ]
    
    def extract(line):
        # Assumes there's only one $ symbol
        dsp = line.index('$')
    
        # Find last index
        last_index = dsp - 1
        while not line[last_index].isdigit():
            last_index -= 1
    
        # Find first index
        first_index = last_index
        while line[first_index-1].isdigit():
            first_index -= 1
    
        return line[first_index:last_index+1]
    
    for line in lines:
        print(extract(line))
    

    结果:

    '1801 (150@$5): 1801'       => 150
    '0055 30 @ $5: 0055'        =>  30
    'leaver - 8 @ $10: 8'       =>   8
    'ATS-55) - 45/$2: 55',150   =>  45
    

    注意返回值 extract() 是字符串,您可能希望将其转换为int。

        3
  •  0
  •   Wiktor Stribiżew    6 年前

    您可以提取一个数字序列(使用 \d+ )后跟0+空格字符和 @ / (使用 (?=\s*[@/]) 向前看)。注意您可以更新 [@/] 字符类,以包含更多定界字符,或添加 |... 如果分隔字符串是一个字符序列:

    import re
    lst = ['1801 (150@$5)', '0055 30 @ $5', 'leaver - 8 @ $10', 'ATS-55) - 45/$2', 'Not solvable']
    for s in lst:
        m = re.search(r'\d+(?=\s*[@/])', s)
        if m:
            print("{}: {}".format(s, m.group()))
        else:
            print("{}: unsolvable".format(s))
    

    请参见python演示,输出:

    1801 (150@$5): 150
    0055 30 @ $5: 30
    leaver - 8 @ $10: 8
    ATS-55) - 45/$2: 45
    Not solvable: unsolvable
    

    图案细节

    • d+ -1+位
    • ?=\s*[@/]) -一种正面的展望,断言存在(需要存在)
      • \s* -0个或更多空白字符
      • [@/] -A @ / 字符。