正则表达式用任何符号解析货币 [英] Regular Expression to parse a currency with any symbol

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

问题描述

您好,


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

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

推荐答案

嘿Mike,


你的正则表达式比实际上要多得多试图找到。

以下应该更符合你想要找到的方向:


@" \(\d {1,3 }:*(?: \.\d +)\)| - \d {1,3}((,\d {3}?):???,\d {3})* (?:\。\ d +)?" ;;


请记住\d *,* \ d * \。?\d *将比赛

234234 ,,,,,,,,, 91872391279379123791827392173912379 172398.82163816238716238

,这可能不是你想要的。如果你想

允许任何使用千分隔符的格式你不需要

也可以使用:


?@" \(????\d {1,3}(:,\d {3})*(?: \.\d +)\)| - \d {1, 3}(?:,?\d {3})*(?:\。\ d +)?" ;;


另请注意我基本上检查了( )表示法然后为

- 表示法。你的正则表达式也会找到:


(-99或-99)


Jesse Houwing


Mike9900写道:
Hey Mike,

Your regex matches a lot more than you''re actually trying to find. The
following should be more in the general direction you''re trying to find:

@"\(\d{1,3}(?:,\d{3})*(?:\.\d+)?\)|-?\d{1,3}(?:,\d{3})*(?:\.\d+)?";

Do keep in mind that \d*,*\d*\.?\d* will match
234234,,,,,,,,,91872391279379123791827392173912379 172398.82163816238716238
as well, which is probably not what you''re looking for. If you want to
allow any format that is using either the thousand separator or not you
could also use this:

@"\(\d{1,3}(?:,?\d{3})*(?:\.\d+)?\)|-?\d{1,3}(?:,?\d{3})*(?:\.\d+)?";

Also notice that I basically check for the () notation and then for the
- notation. Your regex would also have found:

(-99 or -99)

Jesse Houwing

Mike9900 wrote:

您好,


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

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

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


我已经创建了一个正则表达式,但是如果

这个数字是Pound66.99它似乎不起作用但是如果符号在数字之后它会起作用:


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.Su) ccess)

{

十进制结果;

//尝试解析为十进制正常

if(十进制。 TryParse(match.Groups [0] .ToString(),

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

返回结果;

}

返回0;

} -

Mike
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


Mike9900写道:
Mike9900 wrote:

您好,


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

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

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


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

这个数字是Pound66.99但是如果符号是在数字:


publ ic static Decimal ConvertToDecimal(String str)

{

String pattern = @" ^ \(? - ?[\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。任何,空,结果))

返回结果;

}

返回0;

} -

Mike
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


这样做不行,因为我不知道货币符号。这可能是其他任何其他的,例如FR-888.22。

-

Mike

" ;约翰B"写道:
This does not work, because I do not know the currency symbol. It could be
franc ro any other, such as FR-888.22.
--
Mike
"John B" wrote:

Mike9900写道:
Mike9900 wrote:

你好,


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

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

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


我创建了一个正则表达式,但它似乎无法工作如果

数字是Pound66.99,但如果符号在数字后面则有效:


公共静态十进制ConvertToDecimal(String str)

{

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

System.Text.RegularExpressions.Match match

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

if(match 。成功)

{

十进制结果;

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

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

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

返回结果;

}

返回0;

} -

Mike
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


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆