Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Perl regex

Posted on 2012-03-29
4
Medium Priority
?
438 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 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

636 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