Connecting Tech Pros Worldwide Forums | Help | Site Map

Regular Expression for currency

hellbent4u@gmail.com
Guest
 
Posts: n/a
#1: Mar 29 '07
Hi All,

I need a regular expression to be used in an ASP page which would
allow a user to enter:

1000 as well as
1,000

i.e. an amount without comma as well as with comma(for example 10,000
or 100,000 etc.)

If anyone's aware, please let me know. I am in urgent need to use it.

Thanks and Regards,
Ankur


Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#2: Mar 29 '07

re: Regular Expression for currency


Ankur,

Do you have to use a regular expression here? It seems like you have to
use a number, so why not use the TryParse method on the Int32 class? You
can set it to recognize thousands separators, or skip them (using the
NumberFormatInfo class) and then go from there.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

<hellbent4u@gmail.comwrote in message
news:1175139793.519997.194810@l77g2000hsb.googlegr oups.com...
Quote:
Hi All,
>
I need a regular expression to be used in an ASP page which would
allow a user to enter:
>
1000 as well as
1,000
>
i.e. an amount without comma as well as with comma(for example 10,000
or 100,000 etc.)
>
If anyone's aware, please let me know. I am in urgent need to use it.
>
Thanks and Regards,
Ankur
>

Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Posts: n/a
#3: Mar 29 '07

re: Regular Expression for currency


Hi,


The ugliest code ever:

try
{
double d = Convert.toDouble( mycurvalue.Replace( "$", "").Replace(
",","") );

}

Hope you like it :)


<hellbent4u@gmail.comwrote in message
news:1175139793.519997.194810@l77g2000hsb.googlegr oups.com...
Quote:
Hi All,
>
I need a regular expression to be used in an ASP page which would
allow a user to enter:
>
1000 as well as
1,000
>
i.e. an amount without comma as well as with comma(for example 10,000
or 100,000 etc.)
>
If anyone's aware, please let me know. I am in urgent need to use it.
>
Thanks and Regards,
Ankur
>

hellbent4u@gmail.com
Guest
 
Posts: n/a
#4: Mar 30 '07

re: Regular Expression for currency


On Mar 30, 1:17 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Quote:
Ankur,
>
Do you have to use a regular expression here? It seems like you have to
use a number, so why not use the TryParse method on the Int32 class? You
can set it to recognize thousands separators, or skip them (using the
NumberFormatInfo class) and then go from there.
>
Hope this helps.
>
--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com
>
<hellben...@gmail.comwrote in message
>
news:1175139793.519997.194810@l77g2000hsb.googlegr oups.com...
>
>
>
Quote:
Hi All,
>
Quote:
I need a regular expression to be used in an ASP page which would
allow a user to enter:
>
Quote:
1000 as well as
1,000
>
Quote:
i.e. an amount without comma as well as with comma(for example 10,000
or 100,000 etc.)
>
Quote:
If anyone's aware, please let me know. I am in urgent need to use it.
>
Quote:
Thanks and Regards,
Ankur- Hide quoted text -
>
- Show quoted text -
Hi again,

Thanks for your response. But I actually need to use regular
expression on aspx page itself so that the user if wishes to enter an
amount with thousand separator, he is allowed to do so. Also the
simple form(not having commas). Please help me with this if you can.

Regards,
Ankur

Mihai N.
Guest
 
Posts: n/a
#5: Mar 30 '07

re: Regular Expression for currency


Thanks for your response. But I actually need to use regular
Quote:
expression on aspx page itself so that the user if wishes to enter an
amount with thousand separator, he is allowed to do so. Also the
simple form(not having commas). Please help me with this if you can.
And if you will have to localize your web site, or accept money from outside
US, you will run into troubles (because the decimal separator is comma in
most of Europe). TryParse is the safest option.


--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Ben Voigt
Guest
 
Posts: n/a
#6: Mar 30 '07

re: Regular Expression for currency



<hellbent4u@gmail.comwrote in message
news:1175139793.519997.194810@l77g2000hsb.googlegr oups.com...
Quote:
Hi All,
>
I need a regular expression to be used in an ASP page which would
allow a user to enter:
>
1000 as well as
1,000
>
i.e. an amount without comma as well as with comma(for example 10,000
or 100,000 etc.)
Something like:

[0-9]+(,[0-9]{3})*

As others have said, this is only good with the (American? Imperial?)
notation using comma for thousands grouping and point as decimal separator.
Quote:
>
If anyone's aware, please let me know. I am in urgent need to use it.
>
Thanks and Regards,
Ankur
>

hellbent4u@gmail.com
Guest
 
Posts: n/a
#7: Apr 3 '07

re: Regular Expression for currency


On Mar 30, 8:59 pm, "Ben Voigt" <r...@nospam.nospamwrote:
Quote:
<hellben...@gmail.comwrote in message
>
news:1175139793.519997.194810@l77g2000hsb.googlegr oups.com...
>
Quote:
Hi All,
>
Quote:
I need aregularexpressionto be used in an ASP page which would
allow a user to enter:
>
Quote:
1000 as well as
1,000
>
Quote:
i.e. an amount without comma as well as with comma(for example 10,000
or 100,000 etc.)
>
Something like:
>
[0-9]+(,[0-9]{3})*
>
As others have said, this is only good with the (American? Imperial?)
notation using comma for thousands grouping and point as decimal separator.
>
>
>
>
>
Quote:
If anyone's aware, please let me know. I am in urgent need to use it.
>
Quote:
Thanks and Regards,
Ankur- Hide quoted text -
>
- Show quoted text -


Hi Ben,

I tried your regular expression. It worked for most of the cases. But
in addition to all, it also accepts 1000,000 which it should not have.
Instead 1,000,000 is what should have been allowed. Any minor change
that can be done to the reg-ex you provided??

Secondly, I never posted my efforts on this thread. Here's what I have
been trying my hands on :

((\d{1,3})(\,\d{3})*)|(\d\d*))((\.\d{2})?)

It's an OR of two reg-ex, first one being the reg-ex to recognise the
number with thousand separator and second without thousand separator.
The pipe "|" doesn't seem to OR the two conditions. Any idea why is
this happening?

Hoping to get some headway..

Regards,
Ankur

Kevin Spencer
Guest
 
Posts: n/a
#8: Apr 3 '07

re: Regular Expression for currency


Try the following:

\b(?:\d{1,3}(?(?=[,.])[,.](?:\d{3}(?:[.,](?=\d)|\b)|(?:\b|\d{1,2}))|\b))+

It's a bit complicated to explain, but I'll try.

First, it asserts that the match must begin on a word boundary. The match
follows as a non-capturing group which may be repeated any number of times.
The match consists of:

1. 1-3 digits
2. These digits may be followed by a period or comma.
a. If they are followed by a period or a comma, they are followed by
either:
aa. Exactly 3 digits followed by a period or comma that must be
followed by a (not captured) digit, or
bb. a word boundary
b. Otherwise (not followed by a period or comma) either
aa. 1 or 2 digits, or
bb. a word boundary
3. If the initial 1-3 digits are NOT followed by a period or comma, they
must
be followed by a word boundary.

So, essentially, the rules expressed are that a match consists of a sequence
of 1-3 digits with commas between the groups of digits. However, if a group
of digits is surrounded by periods or commas, there must be exactly 3.

The trick is the quantifier on the entire group. This creates a repeating
pattern of groups of 1-3 digits followed by periods or commas. However, the
last group of digits must be followed by a word boundary, and the first
group must be preceded by a word boundary. And if a group of digits is
between periods/commas, there must be exactly 3.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

<hellbent4u@gmail.comwrote in message
news:1175571737.035102.209140@l77g2000hsb.googlegr oups.com...
Quote:
On Mar 30, 8:59 pm, "Ben Voigt" <r...@nospam.nospamwrote:
Quote:
><hellben...@gmail.comwrote in message
>>
>news:1175139793.519997.194810@l77g2000hsb.googleg roups.com...
>>
Quote:
Hi All,
>>
Quote:
I need aregularexpressionto be used in an ASP page which would
allow a user to enter:
>>
Quote:
1000 as well as
1,000
>>
Quote:
i.e. an amount without comma as well as with comma(for example 10,000
or 100,000 etc.)
>>
>Something like:
>>
>[0-9]+(,[0-9]{3})*
>>
>As others have said, this is only good with the (American? Imperial?)
>notation using comma for thousands grouping and point as decimal
>separator.
>>
>>
>>
>>
>>
Quote:
If anyone's aware, please let me know. I am in urgent need to use it.
>>
Quote:
Thanks and Regards,
Ankur- Hide quoted text -
>>
>- Show quoted text -
>
>
>
Hi Ben,
>
I tried your regular expression. It worked for most of the cases. But
in addition to all, it also accepts 1000,000 which it should not have.
Instead 1,000,000 is what should have been allowed. Any minor change
that can be done to the reg-ex you provided??
>
Secondly, I never posted my efforts on this thread. Here's what I have
been trying my hands on :
>
((\d{1,3})(\,\d{3})*)|(\d\d*))((\.\d{2})?)
>
It's an OR of two reg-ex, first one being the reg-ex to recognise the
number with thousand separator and second without thousand separator.
The pipe "|" doesn't seem to OR the two conditions. Any idea why is
this happening?
>
Hoping to get some headway..
>
Regards,
Ankur
>

Closed Thread