[Python] How to create a Regex pattern to change selected digits in a string

Discussão em 'Python' iniciado por Stack, Agosto 23, 2025.

  1. Stack

    Stack Membro Participativo

    I have this string "303000~303000 : ", numbers_303000_303000), and would like to replace only selected digits using RegEx, so the string would look like this: "503000~503000 : ", numbers_503000_503000).

    I have researched many resources on how to create filters with Regex and I am confused how to get this done.

    I have tried to use Regex in Python, and use the following pattern, but it just captures all digits '3'.

    s = '"303000~303000 : ", numbers_303000_303000)'
    re.sub(r'3', r'5', s)


    Result: "502000~505000 : ", numbers_502000_505000)

    Continue reading...

Compartilhe esta Página