Solved

Perl regex

Posted on 2012-03-29
4
414 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linux Real Time Memory on Distinct 24 Cycle 7 46
Perl script to process a .csv file 18 66
SSL/TLS - openssl troubleshooting 3 63
Can't connect to FTP 18 111
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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

730 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