本文介绍了正则表达式用任何符号解析货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我需要一个正则表达式来匹配货币及其符号,例如

Pound66.99必须返回66.99或磅(66.99) )或Pound-66.99或-66.99Pound

返回-66.99或任何其他组合返回十进制数。


我创建了一个正则表达式,但它似乎它不起作用如果

这个数字是Pound66.99但是如果符号在数字之后它就有效:


public static Decimal ConvertToDecimal(String str)

{

字符串模式= @" ^ \(? - ?[\d] * [,] * [\ d] * \ 。?[\ d] * \)?" ;;

System.Text.RegularExpressions.Match match

= System.Text.RegularExpressions.Regex.Match( str,pattern);

if(match.Success)

{

十进制结果;

//试试通常解析为十进制

if(decimal.TryParse(match.Groups [0] .ToString(),

System.Globalization.NumberStyles.Any,null,out result ) )

返回结果;

}

返回0;

} -

迈克

Hello,

I need a regular expression to match a currency with its symbol, for example
Pound66.99 must return 66.99 or Pound(66.99) or Pound-66.99 or -66.99Pound
return -66.99 or any other combination return the decimal number.

I have created a regular expression, but it seems that it does not work if
the number is Pound66.99 but it works if the sign is after the number:

public static Decimal ConvertToDecimal(String str)
{
String pattern = @"^\(?-?[\d]*[,]*[\d]*\.?[\d]*\)?";
System.Text.RegularExpressions.Match match
=System.Text.RegularExpressions.Regex.Match(str, pattern);
if (match.Success)
{
decimal result;
//try to parse to decimal normally
if (decimal.TryParse(match.Groups[0].ToString(),
System.Globalization.NumberStyles.Any, null, out result))
return result;
}
return 0;
}--
Mike

推荐答案




或者完全不同的解决方案:


if(currencyString.IndexOf(" ; poundSign")!= -1)

{

currencyString = currencyString.Replace(" poundSign& ,";

}

....像以前一样使用tryparse

Or a totally different solution:

if(currencyString.IndexOf("poundSign") != -1)
{
currencyString = currencyString.Replace("poundSign", "");
}
....tryparse as before




或完全不同的解决方案:


if(currencyString.IndexOf(" poundSign")!= -1)

{

currencyString = currencyString.Replace(" poundSign","") ;

}

....如前所述tryparse


Or a totally different solution:

if(currencyString.IndexOf("poundSign") != -1)
{
currencyString = currencyString.Replace("poundSign", "");
}
....tryparse as before


这篇关于正则表达式用任何符号解析货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 22:17