Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Location
    Bangalore, India
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Using regular expression in perl

    Hello Guys,

    Sorry, I am not sure if this thread belongs here.
    I am new to perl and writing a regex for identifying if the user input lies in the set [0,100), i.e. the number must lie between 0-100, 0 included. It can be of form 0.000001, .00001, 90, 90.999153263, .12 etc
    Following is the regex I wrote: $input =~ /\d\d?\.\d+|(\d\d?|\.\d+)/, but I am not gettinh the result. Can someone help me in this? Thanks in advance.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by viczsaurav View Post
    Hello Guys,

    Sorry, I am not sure if this thread belongs here.
    I am new to perl and writing a regex for identifying if the user input lies in the set [0,100), i.e. the number must lie between 0-100, 0 included. It can be of form 0.000001, .00001, 90, 90.999153263, .12 etc
    Following is the regex I wrote: $input =~ /\d\d?\.\d+|(\d\d?|\.\d+)/, but I am not gettinh the result. Can someone help me in this? Thanks in advance.
    try this:
    Code:
    /^(\d{0,2}\.\d*)$/
    best regards

  • #3
    New to the CF scene
    Join Date
    Mar 2011
    Location
    Bangalore, India
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks for the prompt reply.
    I have a confusion, in the code /^(\d{0,2}\.\d*)$/ if I am correct, it will search for exact one decimal(.) in such scenario, an integer given as input 10, 25 which does not contain decimal will be marked as invalid.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by viczsaurav View Post
    Thanks for the prompt reply.
    I have a confusion, in the code /^(\d{0,2}\.\d*)$/ if I am correct, it will search for exact one decimal(.) in such scenario, an integer given as input 10, 25 which does not contain decimal will be marked as invalid.
    yes you are right. if you want to add integers and exclude dot try this way:
    Code:
    /^(\d{0,2}(\.\d*)?)$/
    best regards

  • Users who have thanked oesxyl for this post:

    viczsaurav (03-17-2011)

  • #5
    New to the CF scene
    Join Date
    Mar 2011
    Location
    Bangalore, India
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thant was Superlike..Thanks a lot..was trying this for long and each time either it allowed the numbers greater than 100 to pass through or did not allow .123 to pass.
    Thanks again. It has helped me in better understanding of the regex.

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by viczsaurav View Post
    Thant was Superlike..Thanks a lot..was trying this for long and each time either it allowed the numbers greater than 100 to pass through or did not allow .123 to pass.
    Thanks again. It has helped me in better understanding of the regex.
    you are welcome,

    best regards

  • #7
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by oesxyl View Post
    try this way:
    Code:
    /^(\d{0,2}(\.\d*)?)$/
    This thread is done, but I just want to point out two potential issues with this regex.

    1) The single '.' will pass
    2) You use an optional subexpression for the decimal portion, but didn't make it non-capturing.

    Anyway, plenty of ways to address the fist, but maybe a zero width look ahead would work.
    Code:
    /^(?!\.$)(\d{0,2}(?:\.\d*)?)$/
    - Miller

  • Users who have thanked miller for this post:

    viczsaurav (03-28-2011)

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by miller View Post
    This thread is done, but I just want to point out two potential issues with this regex.

    1) The single '.' will pass
    2) You use an optional subexpression for the decimal portion, but didn't make it non-capturing.

    Anyway, plenty of ways to address the fist, but maybe a zero width look ahead would work.
    Code:
    /^(?!\.$)(\d{0,2}(?:\.\d*)?)$/
    - Miller
    yes, you are right, , thank you for posting
    what about:
    Code:
    /^(\d{1,2}(\.\d*)?)$/
    i understand the issue 1) but not sure i understand 2), maybe can you give an example?

    best regards

  • #9
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,872
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Personally, I wouldn't use a regex.

    To me this is cleaner, more understandable (especially for beginners), and more explicit.
    Code:
    if ( $input >= 0 and $input <= 100 ) {
        # do something
    }
    else {
        # do something else
    }

  • #10
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by FishMonger View Post
    Personally, I wouldn't use a regex.
    To me this is cleaner, more understandable (especially for beginners), and more explicit.
    Code:
    if ( $input >= 0 and $input <= 100 ) {
        # do something
    }
    else {
        # do something else
    }
    I agree fishmonger. That was my first thought when seeing this question as well, but of course that doesn't validate for input such as "56foo". I therefore just treated this as an exercise. Heck, I wouldn't have even seen this question if not for the spammer this morning.

    I also decided to make allowances that they might be using regex's specifically for taint checking.

    Quote Originally Posted by oesxyl View Post
    yes, you are right, , thank you for posting
    what about:
    Code:
    /^(\d{1,2}(\.\d*)?)$/
    i understand the issue 1) but not sure i understand 2), maybe can you give an example?

    best regards
    That wouldn't pass .991, or any number that led with a decimal.

    As for concern #2, to make code self-documenting, I always make sure that any group that isn't meant to capture uses (?: ... ) instead of ( ... ). Yes, you gotta type 2 extra characters, but it's so much clearer for new people looking at code to determine what the regex intends.

    - Miller

  • #11
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by miller View Post
    I agree fishmonger. That was my first thought when seeing this question as well, but of course that doesn't validate for input such as "56foo". I therefore just treated this as an exercise. Heck, I wouldn't have even seen this question if not for the spammer this morning.

    I also decided to make allowances that they might be using regex's specifically for taint checking.
    i don't think is anybody which not agree fishmonger non-regex solution,
    about '56foo', agree will not validate but will warn about not being numeric, probably this will not help too much somebody who don't build test cases or don't monitor logs or whatever they use.
    Finaly is a matter of experience and habits,

    That wouldn't pass .991, or any number that led with a decimal.
    good catch,

    As for concern #2, to make code self-documenting, I always make sure that any group that isn't meant to capture uses (?: ... ) instead of ( ... ). Yes, you gotta type 2 extra characters, but it's so much clearer for new people looking at code to determine what the regex intends.
    i agree, in my opinion conditionals are confusing for beginners, some times are confusing for advanced too,

    best regards
    Last edited by oesxyl; 03-24-2011 at 01:24 AM.

  • #12
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,872
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use Scalar::Util qw(looks_like_number);
    
    my $input = '56foo';
    
    if ( looks_like_number($input) and $input >= 0 and $input <= 100 ) {
        print 'do something';
    }
    else {
        print 'do something else';
    }
    FYI, Scalar::Util is a core module
    Last edited by FishMonger; 03-24-2011 at 01:45 AM.

  • Users who have thanked FishMonger for this post:

    viczsaurav (03-28-2011)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •