简体   繁体   English

捕获组中的多个单词

[英]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.

相关问题 捕获一组中的多个子字符串 - Capture multiple substrings in one group 正则表达式捕获一组单词后跟一组格式化的数量 - Regular expression to capture a group of words followed by a group of formatted quantities 如何用字符和捕获组替换 dataframe 列中的多个单词 - How to replace a number of words in a dataframe column with a character and the capture group 正则表达式组捕获多个匹配 - Regular expression group capture with multiple matches 以不同的顺序检索多个捕获组? - Retrieve multiple capture group in different order? 仅当它们位于字符串末尾之前或标点符号或 \n 之前时,才捕获捕获组中一行中的所有大写单词 - Capture all capitalized words in a row in a capture group only if they are before the end of the string or if they are before a punctuation mark or \n 捕获单词并重写 - Capture words and rewrite Python:在一个正则表达式中多次使用相同命名的捕获组 - Python: Using same named capture group multiple times in one regex 将捕获组转换为命名捕获组 - Convert capture group to named capture group 如何使用开始和结束词作为模式将 dataframe 列中的多行文本拆分为多列以捕获其间的文本 - how to split a multiline text in dataframe column into multiple columns using start and end words as pattern to capture the the text inbetween
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM