简体   繁体   English

解析字符串以获取特定字符前后的数字

[英]Parse string to get digits before and after particular character

I am trying to parse digits before and after X from this string, but unable to get all the digits. 我正在尝试从此字符串解析X之前和之后的数字,但无法获取所有数字。 Can someone help me pointing out what I am missing here? 有人可以帮我指出我在这里缺少什么吗?

>>> import re
>>> f = "abc_xyz1024X137M4B4abc_xyz"
>>> re.findall(".*\w+(\d+)X(\d+).*", f)
[('4', '137')]

Note that .*\\w+(\\d+)X(\\d+).* first grabs all the 0+ chars as many as possible (the whole string) and then backracks trying to match the subsequent patterns. 请注意, .*\\w+(\\d+)X(\\d+).*首先捕获尽可能多的0+字符(整个字符串),然后尝试进行机架匹配后继的模式。 \\w+ backtracks up to the point where the next char is a digit before X , so the first capturing group only contains the single digit before X , and the second one contains all the digits after X . \\w+回溯到下一个字符是前一个数字的点X ,所以第一个捕获组只包含前的单数位X ,而第二个包含后所有的数字X Check this .*\\w+(\\d+)X(\\d+).* debugger page . 选中.*\\w+(\\d+)X(\\d+).*调试器页面

You should only match and capture the digits, then match the X and then again match and capture the digits. 您应该只匹配并捕获数字,然后匹配X ,然后再次匹配并捕获数字。

You may use 您可以使用

import re
f = "abc_xyz1024X137M4B4abc_xyz"
print(re.findall(r"(\d+)X(\d+)", f))
# => [('1024', '137')]

Or, if you are only interested in a single match: 或者,如果您只对单个比赛感兴趣:

m = re.search(r"(?P<x>\d+)X(?P<y>\d+)", f)
if m:
    print(m.groupdict()) # => {'y': '137', 'x': '1024'}

See the Python demo and the regex demo . 请参阅Python演示regex演示

In this particular example, another option is to split the string on the character "X" . 在此特定示例中,另一个选择是将字符串拆分为字符"X" Then find the last set of consecutive digits in the left half of the split and the first set of consecutive digits in the right half of the split. 然后在拆分的左半部分中找到最后一组连续数字,在拆分的右半部中找到第一组连续数字。

For example: 例如:

import re
f = "abc_xyz1024X137M4B4abc_xyz"

left, right = f.split("X")
print(right)
#137M4B4abc_xyz

print(left)
#abc_xyz1024

print((re.findall('\d+', left)[-1], re.findall('\d+', right)[0]))
#('1024', '137')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM