Solved

Perl regex

Posted on 2012-03-29
4
424 Views
Last Modified: 2012-03-29
Hello experts!

Quick question, I have been searching the web to find a regex to match these possible inputs.

+ - * /

this is what I am using

	if ($op =~ m/\/|\+|\*|\-/) {
                   # do something...
	} else {
		print "Illegal operator: $op\n";
	}

Open in new window


This works if a user enters a single character, but if i put

=-     <--- will match the "-" but not throw an error? I would like it to throw an error
or
=------    <---- will  match all the "------" and I does not throw the error?


Any help would be greatly appreciated!

Thanks in advance.
0
Comment
Question by:I_s
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37782590
Try:

if ($op =~ m#[+*/-]+#) {
...

Open in new window


Be sure to leave the hyphen just before the closing bracket otherwise you will have to escape it ( \- ).
0
 

Author Comment

by:I_s
ID: 37783500
Awesome, thanks.

if (length($op)==1 && $op=~m#[+*/-]+#) {
                # do something...
} else {
		print "Illegal operator: $op\n";
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 37784500
if( $op =~ m#^[-+*/]$# ){
0
 

Author Comment

by:I_s
ID: 37785259
Even better ozo, now I was able to remove the check for length!

Thanks for the post.
0

Featured Post

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Six Sigma Control Plans

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question