Search code examples
pythonpython-3.xpandasregexregexp-replace

Regular expression to find string, then replace it with the same string and an additional character


I have the following dataset:

{ 16812},
{ 16812},
{ 16813},
{ 16819},
{ 16812},
{value 16812},
{value 16812]},

and I need to match and replace occurrences of { number} with { number | }.

I have the following regular expression that works: \{\s\d(?:[\d]*\.\d+|[\d]*)\}

demo: https://regex101.com/r/I4R4Hh/1

however is there a way to replace the match with the same value plus a pipe. the number could be any number up to six digits, so I can't replace with a generic number as I need to maintain the number I have matched.

I am using python 3 with a pandas dataframe


Solution

  • Does something like this work?

    import re
    
    text = "{ 16812}"
    text = re.sub(r'\{(\s\d(?:[\d]*\.\d+|[\d]*))\}', r'{\1 |}', text)
    
    print(text) # { 16812 |}