[英]Capture multiple words in group
I am trying to parse something in Python where I have a string, a number, and then an undetermined amount of strings after the number. 我正在尝试在Python中解析一些东西,其中我有一个字符串,一个数字,然后在数字之后有一个不确定的字符串。 I would like to be able to put all the remaining strings (in this case "D e") into one group for processing later.
我希望能够将所有剩余的字符串(在这种情况下为“ D e”)放入一组,以便稍后进行处理。 This is the code I have:
这是我的代码:
m = re.match(r"(\w+) (\w+) (*)", "de 2166 D e")
What should I be doing to create a group of "D e"? 创建一组“ D e”应该怎么做?
The code is missing .
代码丢失
.
. 。 (
.
match any character except newline) (
.
匹配除换行符以外的任何字符)
>>> m = re.match(r"(\w+) (\w+) (.*)", "de 2166 D e")
# ^
>>> m.groups()
('de', '2166', 'D e')
If you want to extract only the 3rd group, use MatchObject.group
: 如果只想提取第三个组,请使用
MatchObject.group
:
>>> m.group(3)
'D e'
EDIT suggested by Joe Lewis: 乔·刘易斯(Joe Lewis)建议编辑 :
Use following regular expression if you want to accept only digits for the second group. 如果要仅接受第二组数字,请使用以下正则表达式。 (
\\w
-> \\d
) (
\\w
> \\d
)
r"(\w+) (\d+) (.*)"
# ^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.