从字符串中提取2组数字

时间:2022-09-13 00:18:40

I have a tricky python reg ex question I am not able to solve:

我有一个棘手的python reg ex问题我无法解决:

'alabama_bal_188321000_2000_name_variable_nmr_sw.csv'

I need to process strings like the one above and extract the 2 numbers separately: 188321000 and 2000. It is possible that there are 0 or more underscores before the 9 digit number (188321000 in this case). Also, the length of text after 2000 is variable.

我需要像上面那样处理字符串并分别提取2个数字:188321000和2000.在9位数字之前可能有0个或更多个下划线(在这种情况下为188321000)。此外,2000年后的文本长度是可变的。

Essentially I want to extract the 2 set of numbers in that string.

基本上我想在该字符串中提取2组数字。

2 个解决方案

#1


1  

You can try this,

你可以试试这个,

Code:

import re

regex = r"-?\d+"

test_str = "'alabama_bal_188321000_2000_name_variable_nmr_sw.csv'"

matches = re.finditer(regex, test_str)

for matchNum, match in enumerate(matches):
    matchNum = matchNum + 1

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

Results:

Match 1 was found at 13-22: 188321000
Match 2 was found at 23-27: 2000

#2


1  

import re
m = re.search('(\d+)_(\d+)', your_string)
print(m.group(1), m.group(2))

This outputs:

188321000 2000

#1


1  

You can try this,

你可以试试这个,

Code:

import re

regex = r"-?\d+"

test_str = "'alabama_bal_188321000_2000_name_variable_nmr_sw.csv'"

matches = re.finditer(regex, test_str)

for matchNum, match in enumerate(matches):
    matchNum = matchNum + 1

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

Results:

Match 1 was found at 13-22: 188321000
Match 2 was found at 23-27: 2000

#2


1  

import re
m = re.search('(\d+)_(\d+)', your_string)
print(m.group(1), m.group(2))

This outputs:

188321000 2000