Reputation: 3665
Here's is my javascript regex for a city name and it's handling almost all cases except this.
^[a-zA-Z]+[\. - ']?(?:[\s-][a-zA-Z]+)*$
(Should pass)
(Should Fail)
Upvotes: 0
Views: 2093
Reputation: 72857
Try this regex:
^(?:[a-zA-Z]+(?:[.'\-,])?\s?)+$
This does match:
Coeur d'Alene
San Tan Valley
St. Thomas
St. Thomas-Vincent
St. Thomas Vincent
St Thomas-Vincent
St-Thomas
anaconda-deer lodge county
Monte St.Thomas
San. Tan. Valley
Washington, D.C.
But doesn't match:
St.. Thomas
St.. Thomas--Vincent
St.- Thomas -Vincent
St--Thomas
(I allowed it to match San. Tan. Valley
, since there's probably a city name out there with 2 periods.)
How the regex works:
# ^ - Match the line start.
# (?: - Start a non-catching group
# [a-zA-Z]+ - That starts with 1 or more letters.
# [.'\-,]? - Followed by one period, apostrophe dash, or comma. (optional)
# \s? - Followed by a space (optional)
# )+ - End of the group, match at least one or more of the previous group.
# $ - Match the end of the line
Upvotes: 0
Reputation: 664307
This matches all your names from the first list and not those from the second:
/^[a-zA-Z]+(?:\.(?!-))?(?:[\s-](?:[a-z]+')?[a-zA-Z]+)*$/
Multiline explanation:
^[a-zA-Z]+ # begins with a word
(?:\.(?!-))? # maybe a dot but not followed by a dash
(?:
[\s-] # whitespace or dash
(?:[a-z]+\')? # maybe a lowercase-word and an apostrophe
[a-zA-Z]+ # word
)*$ # repeated to the end
To allow the dots anywhere, but not two of them, use this:
/^(?!.*?\..*?\.)[a-zA-Z]+(?:(?:\.\s?|\s|-)(?:[a-z]+')?[a-zA-Z]+)*$/
^(?!.*?\..*?\.) # does not contain two dots
[a-zA-Z]+ # a word
(?:
(?:\.\s?|\s|-) # delimiter: dot with maybe whitespace, whitespace or dash
(?:[a-z]+\')? # maybe a lowercase-word and an apostrophe
[a-zA-Z]+ # word
)*$ # repeated to the end
Upvotes: 1
Reputation: 71
I think the following regexp fits your requirements :
^([Ss]t\. |[a-zA-Z ]|\['-](?:[^-']))+$
On the other hand, you may question the idea of using a regexp to do that... Whatever the complexity of your regexp, there will always be some fool finding a new unwanted pattern that matches...
Usually when you need to have valid city names, it's better to use some geocoding api, like google geocoding API
Upvotes: 0