ProgrammingThis 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.
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:
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?
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!
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
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
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.