Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 796
  • Last Modified:

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..
0
cooljeba
Asked:
cooljeba
  • 3
  • 2
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now