Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regular expressions...

Reply
Thread Tools

Regular expressions...

 
 
DrRo183@gmail.com
Guest
Posts: n/a
 
      04-26-2006
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.

 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      04-26-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      04-26-2006
<(E-Mail Removed)> 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.
 
Reply With Quote
 
DrRo183
Guest
Posts: n/a
 
      04-27-2006
Great thanks to you both.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using regular phone as voip device with regular modem? hygum VOIP 5 03-23-2005 03:51 PM
Connect 2 routers (wireless and regular routers) Dineyar Buhariwala Wireless Networking 1 11-24-2004 01:37 PM
can a strongly named assembly reference a regular assembly? Prasanna Padmanabhan ASP .Net 1 11-19-2003 06:21 AM
Custom Regular Expressions in ASP.net Jay Douglas ASP .Net 3 11-03-2003 08:09 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments