Perl Regular Expression?

Posted on 2011-04-24
Last Modified: 2012-05-11
I need to write a regular expression where the question marks are in the code code is below. The $var1 will be a value where only numbers and a period are ok.

Valid values would be:

Invalid would be any letters, a comma, other characters, etc...
Only numbers and a period are valid.

Please post an answer and explanation of what it does. I'm terrible at regular expressions and in a hurry to finish some code, but still trying to learn them as I go! Thanks!  

if( $var1 ~= ???? ){
    print "true";
    print "false";

Open in new window

Question by:KGNickl
    LVL 39

    Expert Comment

    by:Pratima Pharande
    LVL 39

    Expert Comment

    by:Pratima Pharande
    LVL 84

    Expert Comment

    perldoc -q "How do I determine whether a scalar is a number/whole/integer/float"
    Found in perlfaq4.pod
           How do I determine whether a scalar is a number/whole/integer/float?

           Assuming that you don't care about IEEE notations like "NaN" or
           "Infinity", you probably just want to use a regular expression.

                   if (/\D/)            { print "has nondigits\n" }
                   if (/^\d+$/)         { print "is a whole number\n" }
                   if (/^-?\d+$/)       { print "is an integer\n" }
                   if (/^[+-]?\d+$/)    { print "is a +/- integer\n" }
                   if (/^-?\d+\.?\d*$/) { print "is a real number\n" }
                   if (/^-?(?:\d+(?:\.\d*)?|\.\d+)$/) { print "is a decimal number\n" }
                   if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
                                   { print "a C float\n" }

           There are also some commonly used modules for the task.  Scalar::Util
           (distributed with 5.8) provides access to perl's internal function
           "looks_like_number" for determining whether a variable looks like a
           number.  Data::Types exports functions that validate data types using
           both the above and other regular expressions. Thirdly, there is
           "Regexp::Common" which has regular expressions to match various types
           of numbers. Those three modules are available from the CPAN.

           If you're on a POSIX system, Perl supports the "POSIX::strtod"
           function.  Its semantics are somewhat cumbersome, so here's a "getnum"
           wrapper function for more convenient access.  This function takes a
           string and returns the number it found, or "undef" for input that isn't
           a C float.  The "is_numeric" function is a front end to "getnum" if you
           just want to say, "Is this a float?"

                   sub getnum {
                           use POSIX qw(strtod);
                           my $str = shift;
                           $str =~ s/^\s+//;
                           $str =~ s/\s+$//;
                           $! = 0;
                           my($num, $unparsed) = strtod($str);
                           if (($str eq '') || ($unparsed != 0) || $!) {
                                           return undef;
                           else {
                                   return $num;

                   sub is_numeric { defined getnum($_[0]) }
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    pratima_mcs' solution is close, except that it won't handle your first sample:  1.0001. The correction is small though:


    Open in new window

    As for the explanation:
    ^          Beginning of string
    \d+        One-or-more ( + ) of a digit ( \d )
    (? ... )   Non-capturing group -- treat everything inside as one unit
    \.         Literal period
    \d+        One-or-more ( + ) of a digit ( \d )
    ?          Zero-or-one. Modifies the thing to the left, which in this case is the non-capturing group
    $          End of string

    Open in new window

    LVL 5

    Author Comment

    LVL 74

    Accepted Solution

    Provided answers did not work or where not what I was asking for.
    You can't honestly be serious.
    LVL 5

    Author Comment

    Award kaufmed points. I didn't look at new answers after 11:00 this morning and then just closed it w/ adding my comments. So that answer is good and can be awarded points, just was after I already located the answer earlier in the morning.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
    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…

    734 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

    23 Experts available now in Live!

    Get 1:1 Help Now