Regular expression help

K

Ken McAndrew

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 RegularExpressionValidator
control:

[$]?\d{1,10}([.]\d{2})?

However, when I tried to make a function using this expression, it would not
validate properly, allowing as many numbers after the decimal point as
desired, and also allowing more than 10 numbers for dollars. For now, I've
come up with the following that does validate properly, included in the
function I'm trying to write below:

public static bool isMoney(string pValue)
{
Regex lReg;
lReg = new Regex("\\b[$]?\\d{1,10}[.]\\d{2}\\b");
if (pValue.Trim().Length==0)
{
return true;
}
return lReg.IsMatch(pValue);
}

That will validate the dollar sign and dollar-number range (1-10 digits),
but when I tried to make the cents portion optional, all of the validation
stopped working properly. I also had to add the \b characters to encapsulate
the string, again so it would work properly.

Any assistance in fixing this up would be appreciated. Thanks.
 
M

Michael Bray

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
RegularExpressionValidator control:

not specific help for your specific problem, but here's a tool that can
help you get the right format quicker than building into a program and
running and testing... called Expresso.

http://www.codeproject.com/dotnet/expresso.asp

-mdb
 
B

BMermuys

Hi,
[inline]
Ken McAndrew said:
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 RegularExpressionValidator
control:

[$]?\d{1,10}([.]\d{2})?

Should be:

string rex = "^\\$?\\d{0,10}(\\.\\d{2})?$";

Where:
^ begin of text
$ end of text

Now you can see that the literal $ must be escaped, and so does the literal
period. Because a period means any character except \r\n (if singleline
option is not set) for regex.

You don't need character groups ([]) for a single literal.

HTH
greetings

However, when I tried to make a function using this expression, it would not
validate properly, allowing as many numbers after the decimal point as
desired, and also allowing more than 10 numbers for dollars. For now, I've
come up with the following that does validate properly, included in the
function I'm trying to write below:

public static bool isMoney(string pValue)
{
Regex lReg;
lReg = new Regex("\\b[$]?\\d{1,10}[.]\\d{2}\\b");
if (pValue.Trim().Length==0)
{
return true;
}
return lReg.IsMatch(pValue);
}

That will validate the dollar sign and dollar-number range (1-10 digits),
but when I tried to make the cents portion optional, all of the validation
stopped working properly. I also had to add the \b characters to encapsulate
the string, again so it would work properly.

Any assistance in fixing this up would be appreciated. Thanks.

--

Kenneth S. McAndrew
Software Developer, Information Concepts
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Regular expression. 13
Regular Expression Help 2
Regex with time 3
Regular Expression Niggle 11
Regular expressions in C# 5
regular expression 7
More regular expression woes 9
Regular Expression Help 1

Top