Search code examples
pythonregexstringtextpython-re

How can I replace a string match with part of itself in Python?


I need to process text in Python and replace any occurrence of "[xz]" by "x", where "x" is the first letter enclosed in the brackets, and "z" can be a string of variable length. Note that I do not want the brackets in the output.

For example, "alEhos[cr@e]sjt" should become "alEhoscsjt"

I think re.sub() could be a way to go, but I am not sure how to implement it.


Solution

  • Instead of directly using the re.sub() method, you can use the re.findall() method to find all substrings (in a non-greedy fashion) that begins and ends with the proper square brackets.

    Then, iterate through the matches and use the str.replace() method to replace each match in the string with the second character in the match:

    import re
    
    s = "alEhos[cr@e]sjt"
    
    for m in re.findall("\[.*?\]", s):
        s = s.replace(m, m[1])
    
    print(s)
    

    Output:

    alEhoscsjt