Solved

Perl regex

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now