LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-02-2007, 12:31 PM   #1
craig467
Member
 
Registered: Jun 2005
Location: Maine
Distribution: Red Hat 9
Posts: 65

Rep: Reputation: 15
Thumbs up regex - allow only letters and spaces


I have a form collecting data including a city name. People are not paying attention and I am collecting strange city names. Is there a way to check that only letters and single spaces have been added in the field. I found out how to remove the leading spaces and trailing spaces with this:

Code:
$city =~ s/^ +//;  #Remove leading spaces
$city =~ s/ +$//;  #Remove trailing spaces
but I am having trouble using the m//operator so that it will accept "San Francisco", "Augusta", "Miami" and reject anything with a number, punctuation or multiple spaces in it.

What I have so far is this:

Code:
if ($city !~ m/[a-zA-Z ]+/) {
    #print error message
}
But that is not working. I tried \b[a-zA-Z ]+\b among other things that did not work and so I am stuck, can someone help?
 
Old 03-02-2007, 12:47 PM   #2
Vookimedlo
Member
 
Registered: Jul 2004
Location: Czech Republic - Roudnice nad Labem
Distribution: Debian
Posts: 253

Rep: Reputation: 32
for example:

Code:
if ($city =~m/[ \t]{2,}/ or $city !~m/^[a-zA-Z ]+$/) {
	print "message\n"
}
 
Old 03-02-2007, 03:30 PM   #3
craig467
Member
 
Registered: Jun 2005
Location: Maine
Distribution: Red Hat 9
Posts: 65

Original Poster
Rep: Reputation: 15
That is incredible.

I would love to know how this stuff makes sense to people, I have not caught on to the logic of regex yet, but I want to. Just bought a book called "Regular Expressions The complete Tutorial" by Jan Goyvaerts and am waiting for it to come. I hope that after reading that, it will make more sense!

Thanks again! I really appreciate it.
 
Old 03-02-2007, 04:17 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,391
Blog Entries: 2

Rep: Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900
Quote:
Originally Posted by Vookimedlo
for example:

Code:
if ($city =~m/[ \t]{2,}/ or $city !~m/^[a-zA-Z ]+$/) {
	print "message\n"
}
Not sure what you are trying to filter with that first regex '/[ \t]{2,}/', which I read as 'exactly two tabs, spaces, or combination thereof'. I cannot see how that serves any useful purpose. Can you explain?
I think the original poster can improve the leading/trailing whitespace trimmer with something like
Code:
$city =~ s/^\s+//;  #Remove leading spaces
$city =~ s/\s+$//;  #Remove trailing spaces
This is more general, and removes tabs, etc.

--- rod.
 
Old 03-02-2007, 06:12 PM   #5
craig467
Member
 
Registered: Jun 2005
Location: Maine
Distribution: Red Hat 9
Posts: 65

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by theNbomr
Not sure what you are trying to filter with that first regex '/[ \t]{2,}/', which I read as 'exactly two tabs, spaces, or combination thereof'. I cannot see how that serves any useful purpose. Can you explain?
I think the original poster can improve the leading/trailing whitespace trimmer with something like
Code:
$city =~ s/^\s+//;  #Remove leading spaces
$city =~ s/\s+$//;  #Remove trailing spaces
This is more general, and removes tabs, etc.

--- rod.
Thanks theNbomr, I fixed the leading/trailing spaces and understand your reasoning - thanks for posting.

As for the '/[ \t]{2,}/', I understood it the way you explained it, but just figured it was my inexperience with regex that was not translating it correctly. I entered in what what posted and it does what I needed it to do which was great, but I am sure I could not explain it. This regex still seems a little smoke and mirrors to me, but I am determined to make sense out of it, seems that it will just take some time.
 
Old 03-03-2007, 04:39 AM   #6
Vookimedlo
Member
 
Registered: Jul 2004
Location: Czech Republic - Roudnice nad Labem
Distribution: Debian
Posts: 253

Rep: Reputation: 32
Quote:
Originally Posted by theNbomr
Not sure what you are trying to filter with that first regex '/[ \t]{2,}/', which I read as 'exactly two tabs, spaces, or combination thereof'. I cannot see how that serves any useful purpose. Can you explain?

Craig467 wrote that he want to: "reject anything with a number, punctuation or multiple spaces in it." And regex '/[ \t]{2,}/' catchs all strings that contain 2 or more white characters.
 
Old 03-03-2007, 09:09 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
your match is matching if it finds a character,
you need to match a non-char like so:

Code:
if ($city =~ m/[^a-zA-Z ]+/) {
    #print error message
}
think about it
m/[a-z/ will match 123a because it finds a char.
m/[^a-z]/ will not match "abc" because it doesn't find a non-char.
but will match "abc1" because it finds a non char
 
  


Reply

Tags
perl, regexp


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl regex $ ShaqDiesel Programming 6 08-18-2006 02:40 PM
regex help siyisoy Programming 4 04-07-2006 05:32 AM
Regex Help cmfarley19 Programming 5 03-31-2005 10:13 PM
GNU C++ Regex lumux Programming 5 09-29-2003 10:51 PM
regex stumper Silly22 Programming 4 07-07-2002 05:10 PM


All times are GMT -5. The time now is 12:49 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration