Issue
I'm trying to clean up some data from web scraping.
This is an example of the information I'm working with:
Best Time
Adam Jones (w/ help) (6:34)Best Time
Kenny Gobbin (a) (2:38)Personal Best
Matt Herrera (12:44)No-record
Nick Elizabeth (19:04)
And this is an example of what I'm trying to achieve:
Best Time
Adam Jones (w/ help) (6:34)
Best Time
Kenny Gobbin (2:38)
Personal Best
Matt Herrera (12:44)
No-record
Nick Elizabeth (19:04)
I want to add two new lines after each right parentheses, but as the times are all different, I don't know how I can search and replace it. Also, numbers may sometimes occur outside of the times.
The closest I've come is by searching for numbers inside the parentheses with a colon to separate them, but I don't know how to replace that with the same information.
re.sub(r"\([0-9]+:[0-9]+\)", "\n\n", result)
Does anyone know how I can achieve this?
Solution
You can do it your way with a minimal change. You only have to know about grouping and add \g<0>
right befor \n\n
. You can read about it in the offical documentation in the section about search-and-replace.
re.sub(r"\([0-9]+:[0-9]+\)", "\g<0>\n\n", result)
Here I used group 0 (the match in ()
) to insert it again. Each set of ()
is a group, counted from the left to the right started with 0.
Answered By - mosc9575
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.