[Python] Replace letters inside words that start with and end with a specific letter not...

Discussão em 'Python' iniciado por Stack, Setembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    I am using the following code to replace each non-initial and non-final letter with ! inside words starting and ending with a not containing a anywhere else in the words:

    import re

    txt = 'aba accca azzza wwwwa accca wwasdwaww abmasedmwa'

    regex = re.sub(r'\ba([^a]+)a\b', r'!\1!', txt)
    print(regex)


    However, the output is

    !b! !ccc! !zzz! wwwwa !ccc! wwasdwaww abmasedmwa


    I'd like to get this output:

    a!a a!!!a a!!!a wwwwa a!!!a wwasdwaww abmasedmwa

    Continue reading...

Compartilhe esta Página