Vincenzo Lo Palo
Vincenzo Lo Palo

Reputation: 1381

Regex issue with currency class

Im using very good currency class in c#. Class working good, but when I need convert USD to IDR (Rupian Indonesian) I get this error

 "{lhs: \"1,0 U.S. dollar\",rhs: \"9 708,73786 Indonesian rupiahs\",error: \"\",icc: true}"

Issue is caused from space in 9 708,73786. I think that I need remove space. This is code about class:

   public static class CurrencyConverter
{
    public static string Convert(decimal amount, string from, string to)
    {
        WebClient web = new WebClient();

        string url = string.Format("http://www.google.com/ig/calculator?hl=en&q={0}{1}=?{2}", amount, from.ToUpper(), to.ToUpper());

        string response = web.DownloadString(url);

        Regex regex = new Regex("rhs: \\\"(\\d*(.|,)\\d*)");
        Match match = regex.Match(response);

        return System.Convert.ToDecimal(match.Groups[1].Value).ToString();
    }
}

}

Probarly I need modify regex, but I dont know what change:

new Regex("rhs: \\\"(\\d*(.|,)\\d*)")

This is screenshot in visual studio 2010 (Windows Form)

enter image description here

Upvotes: 0

Views: 393

Answers (2)

JLRishe
JLRishe

Reputation: 101690

I recommend doing this:

Regex regex = new Regex(@"(?<=rhs: \"")(\d[\s,]?)+");
Match match = regex.Match(response);
string value = Regex.Replace(match.ToString(), @"\s", "");
return System.Convert.ToDecimal(value).ToString();

Try it here: http://ideone.com/ySISDU

Upvotes: 1

fge
fge

Reputation: 121730

Try and substitute globally:

`\s(?=[0-9])`

for the empty string, and only then process your input.

(note that \d in .NET languages match any Unicode digit, hence the use of [0-9] instead)

Upvotes: 0

Related Questions