Welcome to WebmasterWorld Guest from 54.221.119.45

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

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

     

Schoolbag

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

10+ Year Member



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

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

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

10+ Year Member



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

Schoolbag

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

10+ Year Member



where should I put the space?

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

meanjeans

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

10+ Year Member



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

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

10+ Year Member



Forgot an asteric...

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

coopster

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

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

10+ Year Member



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

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

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

WebmasterWorld Senior Member 10+ Year Member



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

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

10+ Year Member



Or even better (I think...)

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

Correct me if I'm wrong.

dkubb

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

10+ Year Member



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

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

10+ Year Member



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

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

10+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month