Regular expressions...

D

DrRo183

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.
 
M

Matt Garrish

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
 
A

Anno Siegel

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top