473,385 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Regular Expression to parse a currency with any symbol

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
Jul 5 '06 #1
10 10157
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:
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
Jul 5 '06 #2
Mike9900 wrote:
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
Or a totally different solution:

if(currencyString.IndexOf("poundSign") != -1)
{
currencyString = currencyString.Replace("poundSign", "");
}
....tryparse as before
Jul 6 '06 #3
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 wrote:
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

Or a totally different solution:

if(currencyString.IndexOf("poundSign") != -1)
{
currencyString = currencyString.Replace("poundSign", "");
}
....tryparse as before
Jul 6 '06 #4
Thanks for the help.

This does not work for some numbers, for example if the number is E.999 it
return 999, if the number is e999,323,22.98 it returns 999323.

Please note that I use Decimal.parse with
System.Globalization.NumberStyles.Any afterward, so the most important thing
is that it strips symbol and gives the correct number.

Or should I use the normal loop and strip symbols?
--
Mike
"Jesse Houwing" wrote:
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:
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
Jul 6 '06 #5
Mike9900 wrote:
Thanks for the help.

This does not work for some numbers, for example if the number is E.999 it
return 999, if the number is e999,323,22.98 it returns 999323.
Yeah I made the expression quite strict the option with no digits before
the '.' wasn't in your sample data and 999,333,22 is syntactically
incorrect as the last part should contain 3 digits. These changes would
be quite simple to implement however:
\((?:\d{1,3}(?:,?\d+)*(?:\.\d+)?|\.\d+)\)|-?(?:\d{1,3}(?:,?\d+)*(?:\.\d+)?|\.\d+)
would probably catch these aswell.

Jesse
>
Please note that I use Decimal.parse with
System.Globalization.NumberStyles.Any afterward, so the most important thing
is that it strips symbol and gives the correct number.

Or should I use the normal loop and strip symbols?
Jul 6 '06 #6

Mike9900 wrote:
"John B" wrote:
Mike9900 wrote:
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
Or a totally different solution:

if(currencyString.IndexOf("poundSign") != -1)
{
currencyString = currencyString.Replace("poundSign", "");
}
....tryparse as before
This does not work, because I do not know the currency symbol. It could be
franc ro any other, such as FR-888.22.
So the best thing to do is assume the currency symbol could in fact be
anything at all, since storing a list of all known currencies would be
silly. So do this:

- remove all characters that aren't digits, sign characters, decimal
points.
- look for a sign character, and note it and remove it if found
- parse what remains as a number
- apply the sign

Regex is inappropriate when you know so little about the format of your
input.

--
Larry Lard
Replies to group please
When starting a new topic, please mention which version of VB/C# you
are using

Jul 6 '06 #7
Thank you for the help. It is now working!
--
Mike
"Jesse Houwing" wrote:
Mike9900 wrote:
Thanks for the help.

This does not work for some numbers, for example if the number is E.999 it
return 999, if the number is e999,323,22.98 it returns 999323.

Yeah I made the expression quite strict the option with no digits before
the '.' wasn't in your sample data and 999,333,22 is syntactically
incorrect as the last part should contain 3 digits. These changes would
be quite simple to implement however:
\((?:\d{1,3}(?:,?\d+)*(?:\.\d+)?|\.\d+)\)|-?(?:\d{1,3}(?:,?\d+)*(?:\.\d+)?|\.\d+)
would probably catch these aswell.

Jesse

Please note that I use Decimal.parse with
System.Globalization.NumberStyles.Any afterward, so the most important thing
is that it strips symbol and gives the correct number.

Or should I use the normal loop and strip symbols?
Jul 6 '06 #8
This is another way, thanks. But it is possible to use regular expression.
Please see the message.
--
Mike
"Larry Lard" wrote:
>
Mike9900 wrote:
"John B" wrote:
Mike9900 wrote:
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
>
Or a totally different solution:
>
if(currencyString.IndexOf("poundSign") != -1)
{
currencyString = currencyString.Replace("poundSign", "");
}
....tryparse as before
This does not work, because I do not know the currency symbol. It could be
franc ro any other, such as FR-888.22.

So the best thing to do is assume the currency symbol could in fact be
anything at all, since storing a list of all known currencies would be
silly. So do this:

- remove all characters that aren't digits, sign characters, decimal
points.
- look for a sign character, and note it and remove it if found
- parse what remains as a number
- apply the sign

Regex is inappropriate when you know so little about the format of your
input.

--
Larry Lard
Replies to group please
When starting a new topic, please mention which version of VB/C# you
are using

Jul 6 '06 #9
Mike9900 wrote:
Thank you for the help. It is now working!
You're Welcome :)

Jesse
Jul 6 '06 #10
Mike9900 wrote:
This is another way, thanks. But it is possible to use regular expression.
Please see the message.
Larry was completely correct that without specs you won't get it done
using regular expressions, but after we had the specs clear, it was no
issue :)

Jesse
Jul 7 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
3
by: Mark | last post by:
How do you parse a currency string to a decimal? I'd like to avoid having to parse the number out, removing the $ manually. That sounds like a hack. There are times that this string will be...
1
by: Sreedhar Vankayala | last post by:
Hi, I have a simple textbox in a form page. - If data not available, then user can enter the text "Not available" or "Contact abc person". - If data is available, then user enters the data...
5
by: Navid Azimi | last post by:
What's the best way to parse a currency string to a decimal given the possibility of multiple currencies? That is, my numeric string can be ($12.00) or -£12.00; in either case I want -12.00 to be...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
5
by: Avi Kak | last post by:
Folks, Does regular expression processing in Python allow for executable code to be embedded inside a regular expression? For example, in Perl the following two statements $regex =...
3
by: Lee Grissom | last post by:
I want to parse the following string into 3 parts per match. I almost got it, but my expression has a flaw in it. I tried this, but didn't quite work the way I expect on the second match b/c...
18
by: Boris Yeltsin | last post by:
OK, I have a database table, it has prices of products in it, like so: ProductPrice MONEY ProductIsoCurrencyCode CHAR(3) Now, both CultureInfo and RegionInfo have...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.