Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
How to add spacing between currency and amount which is inside a string?

My string in Ruby contains currency and amount.

I have purchased and item at USD500.80 from online store store 123X87

I am expecting following result

I have purchased and item at USD 500.80 from online store store 123X87

I tried with Regex but not succeed.

str = "I have purchased and item at USD500.80 from online store store 123X87"

str.gsub(/\d+/, ' \0 ')

# wrong result

"I have purchased and item at USD 500 . 80  from online store store  123 X 87 "

I am not sure what currency that string contains. I only know currency and amount do not have space.

str = "I have purchased and item at EUR500.80 from online store store 123X88"

str = "I have purchased and item at GBP500.80 from online store store 123X88"
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I'd change the regex to scan for all known currency codes explicitly. Any over-generalized regex (such as \w+\d+, for example) is bound to break other parts of the string, like order numbers.

str = "I have purchased and item at USD500.80 from online store store 123X87"
str.gsub(/(USD|EUR)(\d+)/, '\1 \2')
# => "I have purchased and item at USD 500.80 from online store store 123X87"
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda