Remover números no meio de uma string

Remover números no meio de uma string

Quero remover decimais em excesso de uma string usando localizar/substituir por regex.

Por exemplo :

<xml_taga>145.3345542123</xml_taga>
<xml_tagb>125.1245471</xml_tagb>
<xml_tagc>42.12</xml_tagc>

Deve ficar assim:

<xml_taga>145.33</xml_taga>
<xml_tagb>125.12</xml_tagb>
<xml_tagc>42.12</xml_tagc>

O mais longe que cheguei foi com essa expressão

(\.\d{3,12})

Qualquer ajuda será apreciada.

Responder1

  • Ctrl+H
  • Encontre o que: (?<=\d\.\d\d)\d+ OU\d\.\d\d\K\d+
  • Substituir com:LEAVE EMPTY
  • Replace all

Explicação:

(?<=        : start lookbehind, make sure we have 
  \d\.\d\d  : a digit, a dot and 2 digits
)           : end lookbehind
\d+         : 1 or more digits

outra alternativa:

\d\.\d\d    : a digit, a dot and 2 digits
\K          : forget what we have seen until this point
\d+         : 1 or more digits
  • Verifique a expressão regular

Resultado para determinado exemplo:

<xml_taga>145.33</xml_taga>
<xml_tagb>125.12</xml_tagb>
<xml_tagc>42.12</xml_tagc>

Responder2

Quero remover decimais em excesso de uma string usando localizar/substituir por regex.

  • Menu "Pesquisar" > "Substituir" (ou Ctrl+ H)

  • Defina "Encontrar o quê" como (\d+\.\d\d).*<.

  • Defina "Substituir por" para\1<

  • Habilite "Expressão regular" e "corresponde à nova linha"

  • Clique em "Substituir tudo"

    Imagem

Antes:

<xml_taga>145.3345542123</xml_taga>
<xml_tagb>125.1245471</xml_tagb>
<xml_tagc>42.12</xml_tagc>

Depois:

<xml_taga>145.33</xml_taga>
<xml_tagb>125.12</xml_tagb>
<xml_tagc>42.12</xml_tagc>

Leitura adicional

informação relacionada