Basically, I have scraped some tennis scores from a website and dumped them into a dictionary. However, the scores of tiebreak sets are returned like "63" and "77". I want to be able to add a starting parenthesis after the 6 or 7 and close the parentheses at the end of the entire number. So a pseudo code example would be:
>>>s = '613'
>>>s_new = addParentheses(s)
>>>s_new
6(13)
The number after the 6 or 7 can be a one or two digit number from 0 onwards (extremely unlikely that it will be a 3 digit number, so I am leaving that possibility out). I tried reading through some of the regex documentation on the Python website but am having trouble incorporating it in this problem. Thanks.
If the strings always start with 6
or 7
, you're looking for something like:
result = re.sub(r"^([67])(\d{1,2})$", r"\1(\2)", subject)
Explain Regex
^ # the beginning of the string
( # group and capture to \1:
[67] # any character of: '6', '7'
) # end of \1
( # group and capture to \2:
\d{1,2} # digits (0-9) (between 1 and 2 times
# (matching the most amount possible))
) # end of \2
$ # before an optional \n, and the end of the
# string
The replacement string "\1(\2)
concatenates capture Group 1 and capture Group 2 between parentheses.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments