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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-02-2007, 12:31 PM
|
#1
|
|
Member
Registered: Jun 2005
Location: Maine
Distribution: Red Hat 9
Posts: 65
Rep:
|
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?
|
|
|
|
03-02-2007, 12:47 PM
|
#2
|
|
Member
Registered: Jul 2004
Location: Czech Republic - Roudnice nad Labem
Distribution: Debian
Posts: 253
Rep:
|
for example:
Code:
if ($city =~m/[ \t]{2,}/ or $city !~m/^[a-zA-Z ]+$/) {
print "message\n"
}
|
|
|
|
03-02-2007, 03:30 PM
|
#3
|
|
Member
Registered: Jun 2005
Location: Maine
Distribution: Red Hat 9
Posts: 65
Original Poster
Rep:
|
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.
|
|
|
|
03-02-2007, 04:17 PM
|
#4
|
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,258
|
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.
|
|
|
|
03-02-2007, 06:12 PM
|
#5
|
|
Member
Registered: Jun 2005
Location: Maine
Distribution: Red Hat 9
Posts: 65
Original Poster
Rep:
|
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.
|
|
|
|
03-03-2007, 04:39 AM
|
#6
|
|
Member
Registered: Jul 2004
Location: Czech Republic - Roudnice nad Labem
Distribution: Debian
Posts: 253
Rep:
|
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.
|
|
|
|
03-03-2007, 09:09 AM
|
#7
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is 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 07:06 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|