Solved

Perl regex

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 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

19 Experts available now in Live!

Get 1:1 Help Now