CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Which pattern match operators should I use?

 



kencl
User

Jun 16, 2001, 5:27 PM

Post #1 of 10 (11305 views)
Which pattern match operators should I use? Can't Post

Hi Folks,

I'm doing a simple pattern match to insure that form input contains only alpha-numeric characters. Wondering which pattern matching operators I should be using? The match is:

Code
if ($$InputPointer{'password'} =~ m/[^a-zA-Z0-9]/operators) { 
&PrintPage("Invalid Password", "", "Passwords may only contain letters or numbers. Please use you browser's BACK button to enter a different password.");
exit;
}

I'm thinking I should use g because I want it to check every character, o because the match won't change the value I am testing, and s because the string being tested is a single word.

Also, does it make a difference which order the operators are specified in? IE is m/pattern/ixm the same as m/pattern/mix?

Advanced thanks.

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!


randor
User

Jun 17, 2001, 4:00 AM

Post #2 of 10 (11300 views)
Re: Which pattern match operators should I use? [In reply to] Can't Post

kencl,

first off, you can use \W to replace ^a-zA-Z0-9

I'm thinking I should use g because I want it to check every character
yes , g means "global" and will check the whole string...

o because the match won't change the value I am testing
i do not know about this operator??
personally i have never used it in a check for password or username. but i do not want to say for sure that it is not needed.

and s because the string being tested is a single word
i believe you are confusing this with the \s operator that is used for verification in the operator. example:
if ($password =~ /\s/g) {print "your password cannot have spaces"}

but then again i may be wrong..


Also, does it make a difference which order the operators are specified in
no, you can put them in ANY order.




I Hope this helps...

perl programmers don't die.. they just start writing a new script.


Guru
stranger

Jun 21, 2001, 1:39 PM

Post #3 of 10 (11287 views)
Re: Which pattern match operators should I use? [In reply to] Can't Post

Use:

if ($$InputPointer{'password'} !~ /[\w\d]+/) {

&PrintPage("Invalid Password", "", "Passwords may only contain letters or numbers. Please use you browser's BACK button to enter a different password.");
exit;
}




mhx
Enthusiast

Jun 21, 2001, 2:19 PM

Post #4 of 10 (11285 views)
Re: Which pattern match operators should I use? [In reply to] Can't Post

Hi Guru,

did you test your code? I guess not. The regex you specified will match any string that contains at least one alphanumeric character. First, the \d is redundant, because it's part of \w. Second, as I said before, you don't check if the whole string is made of alphanumerics. The correct check would be:

Code
if( $InputPointer->{'password'} !~ /^\w+$/ ) { 
...
}

Hope this is clear and helps.

-- Marcus



Guru
stranger

Jun 21, 2001, 2:23 PM

Post #5 of 10 (11284 views)
Re: Which pattern match operators should I use? [In reply to] Can't Post

My code DOES WORK



mhx
Enthusiast

Jun 21, 2001, 2:43 PM

Post #6 of 10 (11282 views)
Re: Which pattern match operators should I use? [In reply to] Can't Post

Hi!

I'm a bit late replying to this, but perhaps it helps.
Regular expressions sometimes look easier than they are. Unless you understand how a regex engine works, you'll rarely understand regexes in all their detail. What you want to do can be easily done by saying:

Code
unless( $InputPointer->{'password'} =~ /^\w+$/) { 
&PrintPage(...);
exit;
}

You don't need any pattern matching modificators, like g, o, s, m, or whatever.
The g modificator makes only sense within loops:

Code
my $string = 'Hello World'; 
print "$1\n" while $string =~ /(\w+)/g;

would print

Code
 
Hello
World

while

Code
my $string = 'Hello World'; 
print "$1\n" if $string =~ /(\w+)/g;

would only print

Code
 
Hello

The o modificator isn't required because your regular expression doesn't contain variables that could be interpolated. It has nothing to do with the changing of the value you are matching.
The s modificator is only required if you want the dot '.' to also match the newline character. This is only useful if you string can contain newlines and you're using the dot '.' to match an arbitrary character.
To the best of my knowledge, the modificator order is not of importance. Also, you can specify modificators more than once. So m/pattern/iiixx is the same as m/pattern/xi.
I hope this helps.

-- Marcus



mhx
Enthusiast

Jun 21, 2001, 3:02 PM

Post #7 of 10 (11282 views)
Re: Which pattern match operators should I use? [In reply to] Can't Post

Guru,


In Reply To
The regex you specified will match any string that contains at least one alphanumeric character.
THAT WAS THE ANSWER TO THE FRIGGIN QUESTION!!!!!!!

No, it wasn't. kencl wanted

In Reply To
to insure that form input contains only alpha-numeric characters

but your regex would also let something like %&12abc$ pass through, althout it does not only contain alphanumerics. Go and try it!

In Reply To
\d IS NOT PART OF \w - go learn some regex.

\d IS PART OF \w.

Code
print '12' =~ /\w+/ ? 'go learn some regex' : 'this will never appear';

If you have a look at the perlre manpage, you will find:

In addition, Perl defines the following:
\w Match a "word" character (alphanumeric plus "_")

You see: alphanumeric! RTFM!

In Reply To
Mine code DOES WORK

No, it doesn't. And I bet I know enough about regexes. At least I know them better than you do.

-- Marcus



Jasmine
Administrator

Jun 21, 2001, 4:33 PM

Post #8 of 10 (11277 views)
Re: Which pattern match operators should I use? [In reply to] Can't Post

"Guru",

mhx is correct, \d is included in \w. \w means alphanumeric. \d means digits only.

It's always a good idea to verify your answers with a manual before trying to defend an incorrect answer with impropriety.


mhx
Enthusiast

Jun 21, 2001, 9:52 PM

Post #9 of 10 (11271 views)
Re: Which pattern match operators should I use? [In reply to] Can't Post

I'm replying to my own post just because I wasn't able to edit it anymore. Smile
I just wanted to apologize for treating 'Guru' kinda rude in my post. (Compared to his one I'm rather polite, but unfortunately 90% of it were taken out and I didn't quote it as a whole.)
But I've been quite upset, because I can't stand if (someone posts wrong code) AND (the same person throws flame-like posts to me when I point out the errors in their post) AND (the person cowardly withdraws). If someone tells me that

Code
=begin cite_i_dont_remember_so_well 
you as a journeyman know jack [..] about regexes
=cut

and hasn't at least verified the mistakes I showed up by having a look at the manpages, I think I have the right to be upset.
I absolutely don't mind if anyone makes mistakes. I had two posts this week where I was writing crap. (Not really crap, but code that could be enhanced...) But I rather rewarded the people who replied to me for showing me that what I did was wrong.
So again, sorry for the rude post.

-- Marcus



monocle
User

Jun 22, 2001, 10:34 AM

Post #10 of 10 (11265 views)
Re: Which pattern match operators should I use? [In reply to] Can't Post

hey, man... no need to be sorry IMHO. I feel that if one is not willing to have faults/errors pointed out then they are not really interested in learning and bettering themself.


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives