/ / Problem Regex z klasą walut - c #, regex

Regex problem z klasą walut - c #, regex

Używam bardzo dobrej klasy walut w C #. Klasa działa dobrze, ale gdy muszę przekonwertować USD na IDR (indonezyjski Rupian), pojawia się ten błąd

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

Problem został spowodowany z kosmosu w 9 708,73786. Myślę, że potrzebuję usunąć miejsce. To jest kod dotyczący klasy:

   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();
}
}

}

Prawdopodobnie potrzebuję zmodyfikować wyrażenie regularne, ale nie wiem, jakie zmiany:

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

To jest zrzut ekranu w Visual Studio 2010 (Windows Form)

wprowadź opis obrazu tutaj

Odpowiedzi:

1 dla odpowiedzi № 1

Polecam to zrobić:

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();

Wypróbuj tutaj: http://ideone.com/ySISDU


0 dla odpowiedzi nr 2

Spróbuj zastąpić globalnie:

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

dla pustego ciągu, a dopiero potem przetwarzaj dane wejściowe.

(zauważ że d w językach .NET pasuje do dowolnej cyfry Unicode, stąd użycie [0-9] Zamiast tego)