Bijan
Bijan

Reputation: 8586

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?

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?

Upvotes: 0

Views: 149

Answers (1)

Rishabh Gupta
Rishabh Gupta

Reputation: 136

Please have a look at the regex in the link: https://regex101.com/r/gN2cH2/2 The python code generated from the link is. This regex captures all the USerIDs, names , activity dates except the heading row.

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.

Upvotes: 2

Related Questions