Replacing end numbers in a string python

user3685742

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.

zx81

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python - replacing string/symbol with numbers

From Dev

Replacing char on the end of string

From Dev

Replacing numbers with letters in string

From Dev

Python replacing numbers in a list

From Dev

Replacing text only at end of string

From Dev

replacing numbers in a string with regex in javascript

From Dev

RegEx fo replacing numbers in a string

From Dev

replacing a character in a string in python

From Dev

Python - Replacing letters in a string?

From Dev

Replacing a substring between start and end point in a string

From Dev

fix error in string where numbers are replacing letters

From Dev

Need to remove numbers at end of a string

From Dev

Replacing groups of numbers from list (python)

From Dev

replacing part of the string in python 2.7

From Dev

Replacing string and characters in front of it in Python

From Dev

Replacing part of a string in a list in python

From Dev

Replacing specific characters in python string

From Dev

Replacing a leading text string with the same string in python

From Dev

Replacing Leading Zeros adds carriage return at the end of the string

From Dev

Replacing spaces with underscores (ignore spaces that beginning or end of the string)

From Dev

Replacing specific numbers with String.Replace or Regex.Replace

From Dev

Replacing part of a string with countdown numbers using the command sed

From Dev

javascript increment numbers at end of alphanumeric string

From Dev

Removing numbers at the end of a string C#

From Dev

Generate string of numbers python

From Dev

Python string of numbers to date

From Dev

Eliminate numbers in string in Python

From Dev

Python string.replace() not replacing certain characters

From Dev

Strange behaviour while replacing string in python

Related Related

  1. 1

    Python - replacing string/symbol with numbers

  2. 2

    Replacing char on the end of string

  3. 3

    Replacing numbers with letters in string

  4. 4

    Python replacing numbers in a list

  5. 5

    Replacing text only at end of string

  6. 6

    replacing numbers in a string with regex in javascript

  7. 7

    RegEx fo replacing numbers in a string

  8. 8

    replacing a character in a string in python

  9. 9

    Python - Replacing letters in a string?

  10. 10

    Replacing a substring between start and end point in a string

  11. 11

    fix error in string where numbers are replacing letters

  12. 12

    Need to remove numbers at end of a string

  13. 13

    Replacing groups of numbers from list (python)

  14. 14

    replacing part of the string in python 2.7

  15. 15

    Replacing string and characters in front of it in Python

  16. 16

    Replacing part of a string in a list in python

  17. 17

    Replacing specific characters in python string

  18. 18

    Replacing a leading text string with the same string in python

  19. 19

    Replacing Leading Zeros adds carriage return at the end of the string

  20. 20

    Replacing spaces with underscores (ignore spaces that beginning or end of the string)

  21. 21

    Replacing specific numbers with String.Replace or Regex.Replace

  22. 22

    Replacing part of a string with countdown numbers using the command sed

  23. 23

    javascript increment numbers at end of alphanumeric string

  24. 24

    Removing numbers at the end of a string C#

  25. 25

    Generate string of numbers python

  26. 26

    Python string of numbers to date

  27. 27

    Eliminate numbers in string in Python

  28. 28

    Python string.replace() not replacing certain characters

  29. 29

    Strange behaviour while replacing string in python

HotTag

Archive