Passing variables to regular expression ?

Hi,

I have this requirement like I need to pass variables to a regular expression like i want to do some validations like a decimal validation for eg.
like I have a decimal number like 1234.123
Here I want the user to enter maximun of 6 integer part and 4 decimal part anything within that is fine.
I can write the regular expression for that.. but like I want to use the same function to validate it accept only 3 integer part and 3 decimal part? I would like to pass arguments like integerpart and decimalpart as the input. So is it anyway possible so that using the same regular expression I can validation multiple decimal formats by passing the integer part and decimal part seperately as validation requirement ?
or is it possible to pass variables in to the regular expression ?

Thanks in advance..
cooljebaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
$i=3;
$d=3;
print "$_\n" for grep /^\d{1,$i}\.\d{0,$d}/, qw( 1234.123  123.123 );
$i=6;
$d=4;
print "$_\n" for grep /^\d{1,$i}\.\d{0,$d}/, qw( 1234.123  123.123 );
0
cooljebaAuthor Commented:
hi ozo,

Is it possible if u can put that in c# ? thanks..
0
mkosbieCommented:
Check this
    private bool ValidateDecimal(double value, int IntegerPart, int DecimalPart)
    {
        Regex r = new Regex("^\\d{1," + IntegerPart + "}\\.\\d{1," + DecimalPart + "}$");
        return r.IsMatch(value.ToString());
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

cooljebaAuthor Commented:
@mkosbie that works perfectly thanks.. could you please explain the reg ex sytanx
Regex("^\\d{1," + IntegerPart + "}\\.\\d{1," + DecimalPart + "}$");

By just breaking it up like what \\d means $ means etc ? It would be great help..

Thanks again
0
mkosbieCommented:
Sure.  There's really two steps going on in creating that Regex that I have disguised as one.  First, I build a string that represents the Regex, then I load it into a Regex object (which interprets the pattern).  So, on the string step, I use string concatenation to insert you're variables (IntegerPart and DecimalPart) into the string.  I also need to escape the \ characters that I plan to use as escape characters in the Regex.  Basically, because the \ character is recognized as an escape character by both the string interpreter and the Regex interpreter, I have to double escape (eg \\d instead of \d).

So, if I were to call ValidateDecimal(123.456, 3, 4), it would make this string:

"^\d{1,3}\.\d{1,4}$"   (where the \ characters are actual slashes in the string)

That expression breaks down as follows:
^ - Matches the beginning of the input.  This makes sure there's nothing before our match
\d - Matches any digit (0-9)
{1,3} - Repeats the previous match between 1 and 3 times
\. - A . character usually means match any character.  Escaping it tells the regex I want to match a literal .
$ - Matches the end of the input.  This makes sure there's nothing after our match
0
cooljebaAuthor Commented:
Thanks man.. that worked !
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.