Replace numbers that are within a word

Asked

Viewed 114 times

1

I want to remove numbers in the middle of letters, but I do not want to remove all numbers from the text, example:

Vini12cius

I want to turn into Vinicius, but without changing a possible CPF that will come after, I created the following regex:

r = re.sub(r'([0-9])','',"")

But he eliminates all the text numbers, even those that are not between characters, I also tried:

r = re.sub(r'([a-z]*[0-9]*[a-z])','',"") 

But I didn’t succeed either.

  • Keeping the [a-z] within the parentheses, you catch them together, making them also replaced. Review your capture group. Taking advantage, what were things like "123foo" or "foo123"? Should replace or not?

  • yes, in these cases can be removed, my problem is in the numbers that are alone, as I do not wish them to be removed

2 answers

4


I prefer to do this with a replacement function:

>>> def f(m):
...     text = m.group()
...     if text.isdigit(): # se for tudo digito
...         return text
...     else:
...         return re.sub(r'\d', '', text)
... 
>>> re.sub(r'\w+', f, '4lfr3do rodr1g0 marc05 12345')
'lfrdo rodrg marc 12345'
  • Thank you very much, it worked!!

  • Do you know how I could do now if I wanted to accomplish otherwise? Ex: 1234678ab910 and turn into : 12345678910

  • Would just change r'\d' for r'\D' @Hvlopes

  • Perfect, thank you friend

1

You can use this Regular Expression: \d+(?=[a-zA-Z]+)|(?<=[a-zA-Z])\d+

In which the demo of Regex101 can be seen.

Code

import re


testes = ("Vini12cius 000.000.000-00",
            "Vini12cius 00000000000",
            "Vinicius12 00000000000",
            "12Vinicius 00000000000",
            "000.000.000-00 Vini12cius",
            "00000000000 Vini12cius",
            "00000000000 Vinicius12",
            "00000000000 12Vinicius")


padrao_regex = re.compile(r"\d+(?=[a-zA-Z]+)|(?<=[a-zA-Z])\d+")
substituicoes = [re.sub(padrao_regex, "", elemento_teste) for elemento_teste in testes]
if substituicoes:
    for substituicao in substituicoes:
        print(substituicao)

Ideone

  • Thank you!! also worked, thank you !!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.