473,386 Members | 1,610 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,386 software developers and data experts.

Regular Expression for currency

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

Mar 29 '07 #1
7 4372
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]
- mv*@spam.guard.caspershouse.com

<he********@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
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

Mar 29 '07 #2
Hi,
The ugliest code ever:

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

}

Hope you like it :)
<he********@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
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

Mar 29 '07 #3
On Mar 30, 1:17 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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:11**********************@l77g2000hsb.googlegr oups.com...
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- 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

Mar 30 '07 #4
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.
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
Mar 30 '07 #5

<he********@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
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.
>
If anyone's aware, please let me know. I am in urgent need to use it.

Thanks and Regards,
Ankur

Mar 30 '07 #6
On Mar 30, 8:59 pm, "Ben Voigt" <r...@nospam.nospamwrote:
<hellben...@gmail.comwrote in message

news:11**********************@l77g2000hsb.googlegr oups.com...
Hi All,
I need aregularexpressionto 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.


If anyone's aware, please let me know. I am in urgent need to use it.
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

Apr 3 '07 #7
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

<he********@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
On Mar 30, 8:59 pm, "Ben Voigt" <r...@nospam.nospamwrote:
><hellben...@gmail.comwrote in message

news:11**********************@l77g2000hsb.googleg roups.com...
Hi All,
I need aregularexpressionto 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.


If anyone's aware, please let me know. I am in urgent need to use it.
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

Apr 3 '07 #8

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

Similar topics

4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
2
by: Ken McAndrew | last post by:
I am trying to write a regular expression to cover US currency, where the dollar sign and cents are optional. The following string worked when I used it in the ValidationExpression field of a...
5
by: George Durzi | last post by:
If I have a regexvalidator in a webform with a validation expression of ^?\d* and I put the text 2d in a text box being validated by this validator, the validator fires. This regex is a simple...
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...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
10
by: Mike9900 | last post by:
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...
5
by: Dilip | last post by:
I am using the .NET regular expressions library to match anything that resembles a price/amount (33.33, 225.44 etc) out of a stream. I have this pattern with me currently: \d+(\.\d*)? This...
2
by: Gulnarambi | last post by:
I have a form in datasheet view with Total and Currency text boxes, in Currency text box I only can enter values of AZN and USD. Form is based on Table and as criterias information fromUnbound...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.