Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取由未知分隔符分隔的字符串中的最后数字_Python_Regex - Fatal编程技术网

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

Python 获取由未知分隔符分隔的字符串中的最后数字,python,regex,Python,Regex,我正在尝试解析标题,但我不太清楚如何分离字符串中的最后一个数字,我想我已经找到了分隔符(使用那些嵌套的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 我的代

我正在尝试解析标题,但我不太清楚如何分离字符串中的最后一个数字,我想我已经找到了分隔符(使用那些嵌套的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))
重新导入
def getSlots(标题):
x=标题.拆分('@'))

如果len(x)假设您要查找的数字始终是美元符号左侧的连续数字集,则以下类似操作似乎有效:

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 '1801 (150@$5): 1801' => 150 '0055 30 @ $5: 0055' => 30 “离场者-8@$10:8”=>8 'ATS-55)-45/$2:55',150=>45
注意
extract()
的返回值是一个字符串,您可能希望使用正则表达式将其转换为int。

并假设所需的数字字符串后面有一个或多个空格、
@
/
字符和最终的
$

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 1801 (150@$5): 150 0055 30 @ $5: 30 离场者-8@$10:8 ATS-55)-45/$2:45
您可以提取一个数字序列(带有
\d+
),该数字序列后跟0+空格字符,后跟
@
/
(使用
(?=\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个或更多空格字符
    • [@/]
      -一个
      @
      /
      字符

delimeter是否始终是从$符号到第一个数字的字符?“…我不太明白如何分离字符串中的最后一个数字”从您的示例中,您没有在查找字符串中的最后一个数字-请澄清一下?
1801 (150@$5): 150
0055 30 @ $5: 30
leaver - 8 @ $10: 8
ATS-55) - 45/$2: 45
Not solvable: unsolvable