在 Python 中使用正则表达式获取值
Get values using regex in Python
如何仅使用正则表达式获取 test1 的值 4 和 5。我有这个字符串:
test1 = -2, 2, 2, 0, 0, 0, 0, 0, 0, 0
test2= 27000, 20000, 30000, 16200, 8000, 8000, 8000, 8000, 8000, 8000
test3 = 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500
test4 = 7000, 7000, 7000, 8000, 8000, 4000, 4000, 4000, 4000, 4000
test5 = 500
我有这个命令:
`\s(test)\s*=(?P<test>\D.*)`
它选择了2组,但我想要值4和5。或者有其他方法吗?我不想使用 pandas.
s="""test1 = -2, 2, 2, 0, 0, 0, 0, 0, 0, 0
test2= 27000, 20000, 30000, 16200, 8000, 8000, 8000, 8000, 8000, 8000
test3 = 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500
test4 = 7000, 7000, 7000, 8000, 8000, 4000, 4000, 4000, 4000, 4000
test5 = 500"""
import re
lines = s.splitlines()
g1 = re.findall('[-0-9]+', lines[3])[1:] # test4
g2 = re.findall('[-0-9]+', lines[4])[1:] # test5
print(g1)
print(g2)
输出:
['7000', '7000', '7000', '8000', '8000', '4000', '4000', '4000', '4000', '4000']
['500']
import re
regex = r"test1\s*=\s*-?\d+,\s*-?\d+,\s*-?\d+,\s*(-?\d+),\s*(-?\d+)"
test_str = YOUR_STR
matches = re.finditer(regex, test_str, re.MULTILINE)
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)))
如果您正在尝试正则表达式,regex101.com 是一个有用的工具。该站点还会为您生成代码。
我从那里得到了上面的代码。 https://regex101.com/r/WMUbLf/1
如何仅使用正则表达式获取 test1 的值 4 和 5。我有这个字符串:
test1 = -2, 2, 2, 0, 0, 0, 0, 0, 0, 0
test2= 27000, 20000, 30000, 16200, 8000, 8000, 8000, 8000, 8000, 8000
test3 = 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500
test4 = 7000, 7000, 7000, 8000, 8000, 4000, 4000, 4000, 4000, 4000
test5 = 500
我有这个命令:
`\s(test)\s*=(?P<test>\D.*)`
它选择了2组,但我想要值4和5。或者有其他方法吗?我不想使用 pandas.
s="""test1 = -2, 2, 2, 0, 0, 0, 0, 0, 0, 0
test2= 27000, 20000, 30000, 16200, 8000, 8000, 8000, 8000, 8000, 8000
test3 = 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500
test4 = 7000, 7000, 7000, 8000, 8000, 4000, 4000, 4000, 4000, 4000
test5 = 500"""
import re
lines = s.splitlines()
g1 = re.findall('[-0-9]+', lines[3])[1:] # test4
g2 = re.findall('[-0-9]+', lines[4])[1:] # test5
print(g1)
print(g2)
输出:
['7000', '7000', '7000', '8000', '8000', '4000', '4000', '4000', '4000', '4000']
['500']
import re
regex = r"test1\s*=\s*-?\d+,\s*-?\d+,\s*-?\d+,\s*(-?\d+),\s*(-?\d+)"
test_str = YOUR_STR
matches = re.finditer(regex, test_str, re.MULTILINE)
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)))
如果您正在尝试正则表达式,regex101.com 是一个有用的工具。该站点还会为您生成代码。
我从那里得到了上面的代码。 https://regex101.com/r/WMUbLf/1