Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Regular expressions... (http://www.velocityreviews.com/forums/t897678-regular-expressions.html)

DrRo183@gmail.com 04-26-2006 08:22 PM

Regular expressions...
 
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.


Matt Garrish 04-26-2006 08:44 PM

Re: Regular expressions...
 

<DrRo183@gmail.com> wrote in message
news:1146082951.694605.139060@v46g2000cwv.googlegr oups.com...
>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



Anno Siegel 04-26-2006 10:51 PM

Re: Regular expressions...
 
<DrRo183@gmail.com> 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.

DrRo183 04-27-2006 12:10 AM

Re: Regular expressions...
 
Great thanks to you both.



All times are GMT. The time now is 04:18 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.