Passing variables to regular expression ?

Posted on 2008-11-09
Last Modified: 2013-12-17

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..
Question by:cooljeba
    LVL 84

    Expert Comment

    print "$_\n" for grep /^\d{1,$i}\.\d{0,$d}/, qw( 1234.123  123.123 );
    print "$_\n" for grep /^\d{1,$i}\.\d{0,$d}/, qw( 1234.123  123.123 );

    Author Comment

    hi ozo,

    Is it possible if u can put that in c# ? thanks..
    LVL 8

    Accepted Solution

    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


    Author Comment

    @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
    LVL 8

    Expert Comment

    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

    Author Closing Comment

    Thanks man.. that worked !

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now