Welcome to WebmasterWorld Guest from 23.22.182.29

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

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

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

Junior Member

10+ Year Member

joined:Jan 19, 2003
posts:63
votes: 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!

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

Administrator

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

joined:July 31, 2003
posts:12533
votes: 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...
3:59 pm on Mar 25, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 19, 2003
posts:63
votes: 0


Yes, there has to be something, the field cannot be empty.
4:20 pm on Mar 25, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 19, 2003
posts:63
votes: 0


where should I put the space?

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

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

New User

10+ Year Member

joined:Mar 17, 2004
posts:3
votes: 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;
}

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

New User

10+ Year Member

joined:Mar 17, 2004
posts:3
votes: 0


Forgot an asteric...

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

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

Administrator

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

joined:July 31, 2003
posts:12533
votes: 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.

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

New User

10+ Year Member

joined:Mar 17, 2004
posts:3
votes: 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?

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

Administrator

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

joined:July 31, 2003
posts:12533
votes: 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.
9:09 pm on Mar 25, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Aug 31, 2001
posts:1357
votes: 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

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

New User

10+ Year Member

joined:Mar 23, 2004
posts:6
votes: 0


Or even better (I think...)

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

Correct me if I'm wrong.

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

Junior Member

10+ Year Member

joined:Apr 22, 2003
posts:46
votes: 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.

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

Preferred Member

10+ Year Member

joined:Jan 4, 2003
posts:365
votes: 5


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 :(? :)

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

Preferred Member

10+ Year Member

joined:Jan 4, 2003
posts:365
votes: 5


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

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members