繁体   English   中英

货币类的正则表达式问题

[英]Regex issue with currency class

我在C#中使用了非常好的货币类。 该课程运行良好,但是当我需要将USD转换为IDR(印尼盾)时,出现此错误

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

问题是由9 708,73786中的空间引起的。 我认为我需要删除空间。 这是关于类的代码:

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

}

大概我需要修改正则表达式,但是我不知道有什么变化:

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

这是Visual Studio 2010(Windows窗体)中的屏幕截图

在此处输入图片说明

我建议这样做:

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

在这里尝试: http : //ideone.com/ySISDU

尝试在全球范围内替代:

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

输入空字符串,然后才处理您的输入。

(请注意,.NET语言中的\\d匹配任何Unicode数字,因此改用[0-9]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM