Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Regex Replace. Comma and Dot in Number

424 views
Skip to first unread message

Shapper

unread,
Jan 12, 2012, 7:06:28 AM1/12/12
to
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

Marcel Müller

unread,
Jan 12, 2012, 9:19:59 AM1/12/12
to
On 12.01.2012 13:06, Shapper wrote:
> 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

Arne Vajhøj

unread,
Jan 14, 2012, 11:31:28 AM1/14/12
to
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

0 new messages