[Python] Replace all occurrences that match regular expression

Discussão em 'Python' iniciado por Stack, Junho 12, 2025.

  1. Stack

    Stack Membro Participativo

    I have a regular expression that searches for a string that contains '.00.' or '.11.':

    .*\.(00|11)\..*


    I want to replace all occurrences that match the pattern with 'X00X' or 'X11X'. For example, the string '.00..0..11.' would result in 'X00X.0.X11X'.

    I was looking into the Python re.sub method and am unsure of how to do this effectively. The returned match object only matches on the first occurrence and therefore doesn't work well. Should I just use a string replace for this task?

    Continue reading...

Compartilhe esta Página