homepage Welcome to WebmasterWorld Guest from 54.161.214.221
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
How do I do this again? (Basic perl Question)
Schoolbag




msg:430153
 3:33 pm on Mar 25, 2004 (gmt 0)

Im forgetting my Perl code:

Okay, the following kills the application if $county is not an alpha character. Now, how can I allow SPACES, for example, the county of "Jersey" would be okay but the county of "New York" keeps getting kicked out. I need both to work.

if (!($county =~ /^[A-Za-z]*$/)) {
&kill_input2;
}

thanks!

 

coopster




msg:430154
 3:43 pm on Mar 25, 2004 (gmt 0)

Just add a space in there. Well, hold on now I guess. If you simply add a space in there, then a single space all by itself in the $county variable would be accepted too, and that's probably not what you want, correct? Easy enough to check for that first though...

Schoolbag




msg:430155
 3:59 pm on Mar 25, 2004 (gmt 0)

Yes, there has to be something, the field cannot be empty.

Schoolbag




msg:430156
 4:20 pm on Mar 25, 2004 (gmt 0)

where should I put the space?

like so?
if (!($county =~ /^[A-Za-z ]*$/)) {

meanjeans




msg:430157
 4:40 pm on Mar 25, 2004 (gmt 0)

Try this one. The editional parenthesis and the pipe create and either/or match.

if (!($county =~ /(^[A-Za-z]*$^[A-Za-z] [A-Za-z]*$)/)) {
&kill_input2;
}

meanjeans




msg:430158
 4:42 pm on Mar 25, 2004 (gmt 0)

Forgot an asteric...

if (!($county =~ /(^[A-Za-z]*$^[A-Za-z]* [A-Za-z]*$)/)) {
&kill_input2;
}

coopster




msg:430159
 4:51 pm on Mar 25, 2004 (gmt 0)

Right on, Schoolbag.

Welcome to WebmasterWorld, meanjeans!

I thought that at first too, but now what do you do about 'Fond Du Lac' county? I would trim leading and trailing whitespace from the $county variable, then use the regex as Schoolbag has it.

meanjeans




msg:430160
 5:00 pm on Mar 25, 2004 (gmt 0)

True. Or you could have a banner on your site that says "If you live in a county with more that two words in it, you can not use this service, I'm very sorry"...

I am a bit of hacker when it comes to programming, thought I'd help out though.

So Schoolbag's second expression says any combo of alpha characters followed by a space, any number of times?

coopster




msg:430161
 5:03 pm on Mar 25, 2004 (gmt 0)

Actually, it doesn't work, I should have tested first. This should do it:
if (!($county =~ /^[A-Za-z]+[A-Za-z ]*$/)) { 
It says, try to match any string that begins with one or more alphabetic characters followed by zero or more alphabetic characters or spaces.
Hissingsid




msg:430162
 9:09 pm on Mar 25, 2004 (gmt 0)

if (!($county =~ /^[A-Za-z]+[A-Za-z ]*$/)) {

Also this works

if ($county!~ /^[A-Za-z]+[A-Za-z ]*$/) {

with slightly less code.

Best wishes

Sid

Aranor




msg:430163
 11:13 pm on Mar 30, 2004 (gmt 0)

Or even better (I think...)

if ($county!~ /^[A-Za-z?]*$/) {

Correct me if I'm wrong.

dkubb




msg:430164
 5:29 am on Mar 31, 2004 (gmt 0)

There's two small problems with some of the other regexes submitted in this thread:

  1. Many of them would match a string with a trailing space. For example: "New York " (without the quotes).
  2. They are using the $ metacharacter which matches the end of a *line*, not the end of a string -- there's a small difference. If the string was "New York\n" many of the regexes submitted would match it. (note the trailing newline)

In most situations these problems are minor and would have no impact, but there are cases where you need an absolute perfect match, so a more strict approach is necessary:

if($county !~ /\A[[:alpha:]]+(?:[[:space:]]+[[:alpha:]]+)*\z/) {

This will only match words containing letters only, optionally with spaces between them, and will not match anything with a trailing space. Also the trailing \z anchor matches the end of the *string*, not the end of the line like $ does, so if there's a newline at the end of the string it will not match.

If you'd prefer not to use the POSIX character classes within the regex, here's a second example that does the same thing:

if($county !~ /\A[A-Za-z]+(?: +[A-Za-z]+)*\z/) {

I'm not really a big fan of the above example because of the space in the middle of the regex. Another programmer could come along and totally miss the fact that I put a space there with a "+" quantifier. It would be really easy for this mistake to happen and for them to make an incorrect assumption about what this does.

Here's a third example where I replaced the "hidden" space character with the hexidecimal form of the space character. At least with this example Its more obvious what my intentions are:

if($county !~ /\A[A-Za-z]+(?:\x20+[A-Za-z]+)*\z/) {

In production code I would be inclined to use something more explicit like the above example, or the first regex using the POSIX character classes.

Moby_Dim




msg:430165
 7:06 am on Mar 31, 2004 (gmt 0)

I have not Perl installed today to test, but it seems easy at a first glance:
if (!($county =~ /^[A-Za-z ]{2,}$/))

OR I forgot everything :(? :)

Moby_Dim




msg:430166
 7:09 am on Mar 31, 2004 (gmt 0)

After s/^\s*// and s/\s*$// , of course.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved