Regular expressions...

Discussion in 'Perl Misc' started by DrRo183@gmail.com, Apr 26, 2006.

  1. Guest

    I have to write a script that will act like the 'comm' utility. My
    problem is when trying to read whether the user has entered -123 or -1
    or -1...etc.

    I currently have:

    if(m/[\-][0-3][0-3]?[0-3]?[^a-z]/g){
    print "HOOORAAAAY!\n";
    }

    So, this should check for all -123, -1, -12, -32, etc. But I would like
    to output an error message if the user enters something like:
    -123333

    or

    -123abc

    Something is wrong with the [^a-z] part. Could someone help me clean
    this up? Thanks.
     
    , Apr 26, 2006
    #1
    1. Advertising

  2. Matt Garrish Guest

    <> wrote in message
    news:...
    >I have to write a script that will act like the 'comm' utility. My
    > problem is when trying to read whether the user has entered -123 or -1
    > or -1...etc.
    >
    > I currently have:
    >
    > if(m/[\-][0-3][0-3]?[0-3]?[^a-z]/g){
    > print "HOOORAAAAY!\n";
    > }
    >
    > So, this should check for all -123, -1, -12, -32, etc. But I would like
    > to output an error message if the user enters something like:
    > -123333
    >
    > or
    >
    > -123abc
    >
    > Something is wrong with the [^a-z] part.


    Nothing is wrong with that part; it's a perfectly valid negated character
    class. It will find any single character that is not one of those 26
    (including another digit). What exactly are you trying to match?

    As for the other part of the problem, anchor the pattern:

    /^-?[0-3]{1,3}$/

    This will restrict it to matching exactly 1-3 instances with an optional
    minus sign.

    Matt
     
    Matt Garrish, Apr 26, 2006
    #2
    1. Advertising

  3. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > I have to write a script that will act like the 'comm' utility. My
    > problem is when trying to read whether the user has entered -123 or -1
    > or -1...etc.
    >
    > I currently have:
    >
    > if(m/[\-][0-3][0-3]?[0-3]?[^a-z]/g){
    > print "HOOORAAAAY!\n";
    > }
    >
    > So, this should check for all -123, -1, -12, -32, etc. But I would like
    > to output an error message if the user enters something like:
    > -123333
    >
    > or
    >
    > -123abc


    So apparently you want to allow one to three of the digits 1, 2, 3 (why
    do you include 0 in your regex?), but no other characters. That's easily
    checked with a regex:

    /^-[123]{1,3}$/

    But you also want to disallow duplicates, which is hard to do with a regex.
    Use something else for that. A hash comes to mind.

    My comm(1) doesn't complain about duplicates, btw.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Apr 26, 2006
    #3
  4. DrRo183 Guest

    Great thanks to you both.
     
    DrRo183, Apr 27, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas

    Custom Regular Expressions in ASP.net

    Jay Douglas, Nov 2, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    608
    mikeb
    Nov 3, 2003
  2. mark

    Regular expressions

    mark, Jun 30, 2003, in forum: Perl
    Replies:
    4
    Views:
    1,722
  3. Dustin D.
    Replies:
    1
    Views:
    11,212
  4. Jay Douglas
    Replies:
    0
    Views:
    609
    Jay Douglas
    Aug 15, 2003
  5. Noman Shapiro
    Replies:
    0
    Views:
    234
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page