I have strings of this kind
我有这种字符串
hello 45 blabla 12 100 code 45 13 093bb
I'd like to match the second number that follows the keyword code
(with Python). In this example "13"
should be returned.
我想匹配关键字代码后面的第二个数字(使用Python)。在此示例中,应返回“13”。
Here is the solution I found
这是我找到的解决方案
s = "hello 45 blabla 12 100 code 45 13 093bb "
re.search("code \d+ \d+",s).group(0).split(" ")[-1]
# '13'
It works but there are probably better solutions. I have tried using lookbehind but I run into the issue that python doesn't tolerate lookbehind of underfined length (and I don't know the number of digits in the first number that follows code
). I tried that
它有效,但可能有更好的解决方案。我尝试过使用lookbehind但是我遇到了python不能容忍欠定长度的问题(我不知道代码后面的第一个数字中的位数)的问题。我试过了
re.search("(?<=code \d+)\d+",s).group(0)
# raise error, v # invalid expression
# sre_constants.error: look-behind requires fixed-width pattern
4 个解决方案
#1
3
you could skip re
all together
你可以一起跳过
>>> a = s.split()
>>> a[a.index('code') + 2]
'13'
#2
1
You could make it slightly cleaner by putting the desired value into a group.
您可以通过将所需的值放入组中来使其更清晰。
>>> re.search("code \d+ (\d+)",s).group(1)
'13'
#3
1
re.search("code \d+ (\d+)", s).group(1)
should work. The () brackets identify which group you want to capture. Group 0 is the whole text and the following are the groups numbered in sequence.
应该管用。 ()括号标识要捕获的组。组0是整个文本,以下是按顺序编号的组。
#4
1
This is a little shorter than the re.search
solutions:
这比re.search解决方案稍短:
re.findall('code \d+ (\d+)', s)
It will return back only what matches the group defined in the ()
它将仅返回与()中定义的组匹配的内容
#1
3
you could skip re
all together
你可以一起跳过
>>> a = s.split()
>>> a[a.index('code') + 2]
'13'
#2
1
You could make it slightly cleaner by putting the desired value into a group.
您可以通过将所需的值放入组中来使其更清晰。
>>> re.search("code \d+ (\d+)",s).group(1)
'13'
#3
1
re.search("code \d+ (\d+)", s).group(1)
should work. The () brackets identify which group you want to capture. Group 0 is the whole text and the following are the groups numbered in sequence.
应该管用。 ()括号标识要捕获的组。组0是整个文本,以下是按顺序编号的组。
#4
1
This is a little shorter than the re.search
solutions:
这比re.search解决方案稍短:
re.findall('code \d+ (\d+)', s)
It will return back only what matches the group defined in the ()
它将仅返回与()中定义的组匹配的内容