简体   繁体   English

正则表达式重复捕获组

[英]RegEx Repeating Capture Group

I have the following data: 我有以下数据:

 User ID              Name                                                         Last Activity Date     
 -------------------- ------------------------------------------------------------ -----------------------
UserID1               UserName1                                                    2016-05-31
UserID2               UserName2                                                    2016-05-31
UserID3               UserName3                                                    2016-05-31
...

What is the best way to use RegEx to capture all the UserIDs, Names, and Activity Dates? 使用RegEx捕获所有用户ID,名称和活动日期的最佳方法是什么?

I currently have User ID\\s+Name\\s+Last Activity Date\\s+[- \\s]+(.*?)\\s+(.*?)\\s{6,}(.*)\\s and this is able to capture the first line but how can I repeat this to get all the other lines? 我目前有User ID\\s+Name\\s+Last Activity Date\\s+[- \\s]+(.*?)\\s+(.*?)\\s{6,}(.*)\\s ,这是能够捕获第一行,但是如何重复此操作以获得所有其他行?

Please have a look at the regex in the link: https://regex101.com/r/gN2cH2/2 The python code generated from the link is. 请查看链接中的正则表达式: https ://regex101.com/r/gN2cH2/2从链接生成的python代码是。 This regex captures all the USerIDs, names , activity dates except the heading row. 此正则表达式捕获除标题行以外的所有USerID,名称,活动日期。

import re
p = re.compile(ur'((UserID[0-9])\s+(UserName[0-9]+)\s+([0-9]{4}-[0-9]{2}-[0-9]{2}))')
test_str = u" User ID              Name                                                         Last Activity Date     \n -------------------- ------------------------------------------------------------ -----------------------\nUserID1               UserName1                                                    2016-05-31\nUserID2               UserName2                                                    2016-05-31\nUserID3               UserName3                                                    2016-05-31"

re.findall(p, test_str)

You can find the values of UserID's , names and capturing and last activity using capturing groups of paranthesis. 您可以使用捕获多组寄生物来找到UserID的值,名称,捕获和上次活动。

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

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