我有一个棘手的python reg ex问题我无法解决:
'alabama_bal_188321000_2000_name_variable_nmr_sw.csv'
我需要像上面那样处理字符串并分别提取2个数字:188321000
和2000
。在9位数字之前可能有0个或更多下划线(在这种情况下为188321000
)。此外,2000
之后的文本长度是可变的。
基本上我想在该字符串中提取2组数字。
答案 0 :(得分:1)
import re
m = re.search('(\d+)_(\d+)', your_string)
print(m.group(1), m.group(2))
输出:
188321000 2000
答案 1 :(得分:1)
你可以试试这个,
代码:
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)))
结果:
Match 1 was found at 13-22: 188321000
Match 2 was found at 23-27: 2000