Regex Replace. Comma and Dot in Number

S

Shapper

Hello,

I need to convert strings as "4.32%" or "4,32%" to "4 32 percent". So I am using:

Regex.Replace(value, @"\s*%\s*", " percent ").

This fails because "4.32%" or "4,32%" does not become "4 32 percent" but "4.32 percent".

I would like to have this done only with Regex.

How can I do this?

Thank You,

Miguel
 
M

Marcel Müller

I need to convert strings as "4.32%" or "4,32%" to "4 32 percent". So I am using:

Regex.Replace(value, @"\s*%\s*", " percent ").
This fails because "4.32%" or "4,32%" does not become "4 32 percent" but "4.32 percent".

Of course, you only replaced the % sign and the surrounding white space
characters.
I would like to have this done only with Regex.

Regex.Replace(value, @"\s*(\d+)[,.](\d\d)\s*%\s*", "$1 $2 percent")

If the replacement fails, your input string has not the expected syntax.


Marcel
 
A

Arne Vajhøj

I need to convert strings as "4.32%" or "4,32%" to "4 32 percent". So I am using:

Regex.Replace(value, @"\s*%\s*", " percent ").

This fails because "4.32%" or "4,32%" does not become "4 32 percent" but "4.32 percent".

I would like to have this done only with Regex.

How can I do this?

Marcel already gave you a regex for this.

But why do you want to use regex?

Based on your very short description then

..Replace(".", " ").Replace(",", " ").replace("%", " percent")

would do.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top