I need a regular Expression for Validating City textBox, the city textbox field accepts only Letters, spaces and dashes(-).
13 Answers
This answer assumes that the letters which @Manaysah refers to also encompasses the use of diacritical marks. I've added the single quote ' since many names in Canada and France have it. I've also added the period (dot) since it's required for contracted names.
Building upon @UIDs answer I came up with,
^([a-zA-Z\u0080-\u024F]+(?:. |-| |'))*[a-zA-Z\u0080-\u024F]*$
The list of cities it accepts:
Toronto St. Catharines San Fransisco Val-d'Or Presqu'ile Niagara on the Lake Niagara-on-the-Lake München toronto toRonTo villes du Québec Provence-Alpes-Côte d'Azur Île-de-France Kópavogur Garðabær Sauðárkrókur Þorlákshöfn
And what it rejects:
A----B ------ ******* && () // \\
I didn't add in the use of brackets and other marks since it didn't fall within the scope of this question.
I've stayed away from \s for whitespace. Tabs and line feeds aren't part of a city name and shouldn't be used in my opinion.
-
2^([a-zA-Z\u0080-\u024F]+(?:(\. )|-| |'))*[a-zA-Z\u0080-\u024F]*$ it should be (\. ) instead of just ". ", because . is a special character – maxisam Feb 25 '16 at 16:59
-
I replaced the last . with a + to disallow a city name ending with a hyphen – mattlary Mar 30 '17 at 19:42
-
3This doesn't work if city contais a number... for example "NICE CEDEX 1" – V. Sambor Sep 13 '17 at 07:40
-
-
Stripping out slashes unfortunately cuts out some cities. For instance, Guelph/Eramosa. – Alex Plumb Jun 26 '23 at 15:04
This can be arbitrarily complex, depending on how precise you need the match to be, and the variation you're willing to allow.
Something fairly simple like ^[a-zA-Z]+(?:[\s-][a-zA-Z]+)*$
should work.
warning: This does not match cities like München, etc, but here you basically need to work with the [a-zA-Z] part of the expression, and define what characters are allowed for your particular case.
Keep in mind that it also allows for something like San----Francisco, or having several spaces.
Translates to something like: 1 or more letters, followed by a block of: 0 or more spaces or dashes and more letters, this last block can occur 0 or more times.
Weird stuff in there: the ?:
bit. If you're not familiarized with regexes, it might be confusing, but that simply states that the piece of regex between parenthesis, is not a capturing group (I don't want to capture the part it matches to reuse later), so the parenthesis are only used as to group the expression (and not to capture the match).
"New York" // passes
"San-Francisco" // passes
"San Fran Cisco" // passes (sorry, needed an example with three tokens)
"Chicago" // passes
" Chicago" // doesn't pass, starts with spaces
"San-" // doesn't pass, ends with a dash
-
+1, but your `[\s-]*` should be `[\s-]+`. That part of the regex shouldn't become active unless an actual hyphen or whitespace character is seen. – Alan Moore Aug 01 '12 at 14:25
-
You're totally right, it makes no sense to have a city ending in spaces or hifens, and that would be allowed by my current regex, changing it... Thanks! - Updated: Actually changed to [\s-] alone, since I don't think we need multiple spaces or hifens without following letters. – pcalcao Aug 01 '12 at 14:30
-
2If you want cities like München to pass, replace `[a-zA-Z]` with `\p{L}` Here's my version: `^\p{Lu}\p{L}*(?:[\s-]\p{Lu}\p{L}*)*$` – Philippe Hebert Nov 22 '16 at 19:41
-
-
What if I want to allow it to start with a space (or multiple spaces)? – Milkncookiez May 05 '20 at 14:08
-
For an explanation on what the `\p{L}` means there's a good answer here: https://stackoverflow.com/questions/14891129/regular-expression-pl-and-pn/14891168 – Pete Jun 02 '21 at 10:41
Adding my answer if anybody needs its while searching for Regex for City Names, Like I did
Please use this :
^[a-zA-Z\u0080-\u024F\s\/\-\)\(\`\.\"\']+$
As many city names contains dashes, such as Soddy-Daisy, Tennessee, or special characters like, ñ in La Cañada Flintridge, California
Hope this helps!

- 4,434
- 11
- 45
- 75
Here is the one I've found works best
for PCRE flavours allowing \p{L}
(.NET, php, Golang)
/^\p{L}+(?:([\ \-\']|(\.\ ))\p{L}+)*$/u
for regex that does not allow \p{L}
replace it with [a-zA-Z\u0080-\u024F]
so for javascript, python regex use
/^[a-zA-Z\u0080-\u024F]+(?:([\ \-\']|(\.\ ))[a-zA-Z\u0080-\u024F]+)*$/
White listing a bunch of character is easy, but there are things to watch for in your regex
- consecutive non-alphabetical characters should not be allowed. i.e.
Los Angeles
should fail because it has two spaces - periods should have a space after. i.e.
St.Albert
should fail because it's missing the space - names cannot start or end with non-alphabetical characters i.e.
-Chicago-
should fail - a whitespace character
\s
!==\
, i.e. a tab and line feed character could pass, so space character should be defined instead
Note: When building regex rules, I find https://regex101.com/tests is very helpful, as you can easily create unit tests
js: https://regex101.com/r/cgJwc0/1/tests
php: https://regex101.com/r/Yo3GV2/1/tests

- 34,125
- 17
- 102
- 150
-
1Working with Golang, I've found your regex more than useful! _Jesus bless you – Dilunga the Great Jan 20 '21 at 20:57
Here's one that will work with most cities, and has been tested:
^[a-zA-Z\u0080-\u024F]+(?:. |-| |')*([1-9a-zA-Z\u0080-\u024F]+(?:. |-| |'))*[a-zA-Z\u0080-\u024F]*$
Python code below, including its test.
import re
import pytest
CITY_RE = re.compile(
r"^[a-zA-Z\u0080-\u024F]+(?:. |-| |')*" # a word
r"([1-9a-zA-Z\u0080-\u024F]+(?:. |-| |'))*"
r"[a-zA-Z\u0080-\u024F]*$"
)
def is_city(value: str) -> bool:
valid = CITY_RE.match(value) is not None
return valid
# Tests
@pytest.mark.parametrize(
"value,expected",
(
("1", False),
("Toronto", True),
("Saint-Père-en-Retz", True),
("Saint Père en Retz", True),
("Saint-Père en Retz", True),
("Paris 13e Arrondissement", True),
("Paris 13e Arrondissement ", True),
("Bouc-Étourdi", True),
("Arnac-la-Poste", True),
("Bourré", True),
("Å", True),
("San Francisco", True),
),
)
def test_is_city(value, expected):
valid, msg = validate.is_city(value)
assert valid is expected

- 25,125
- 19
- 60
- 71
^[a-zA-Z\- ]+$
Also this might be useful http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/

- 187
- 2
- 16
-
1This will only work for English names. `München` (Munich) would not match. Using `\w` instead could help, when used with caution – amon Aug 01 '12 at 10:14
-
-
@burning_LEGION That is where the caution comes in. In Perl I'd write `/((?!\d|_)[\w -])+/` (look-ahead) – amon Aug 01 '12 at 10:17
-
use this regex:
^[a-zA-Z-\s]+$

- 13,246
- 8
- 40
- 52
-
-
Yeah probably best to actually declare the space character ` ` instead of `\s` as if the textbox allows for multi-line, it will parse the newline character as valid. – GoldBishop Aug 05 '13 at 17:41
After many hours of looking for a city regex matcher I have built this and it meets my needs 100%
(?ix)^[A-Z.-]+(?:\s+[A-Z.-]+)*$
expression for testing city. Matches
- City
- St. City
- Some Silly-City
- City St.
- Too Many Words City
it seems that there are many flavors of regex and I built this for my Java needs and it works great

- 31
- 1
- 4
^[a-zA-Z.-]+(?:[\s-][\/a-zA-Z.]+)*$
This will help identify some city names like St. Johns, Baie-Sainte-Anne, Grand-Salut/Grand Falls

- 57
- 7
-
dot should be near \s- ^[a-zA-Z.-]+(?:[\s-.][\/a-zA-Z]+)*$, else it will allow dot at end – Satish Patro May 08 '20 at 14:03
You can try this:
^\p{L}+(?:[\s\-]\p{L}+)*
The above regex will:
- Restrict leading and trailing spaces, hyphens
- Match cities with names like Néewiller-près-lauterbourg

- 34,125
- 17
- 102
- 150

- 121
- 1
- 1
- 3
-
Should be noted that this is the php and golang regex flavour, js and python should use `\u0080-\u024F` instead of `\p{L}` – Daniel May 09 '18 at 19:59
I like shepley's suggestion, but it has a couple flaws in it.
If you change shpeley's regex to this, it will not accept other special characters:
^([a-zA-Z\u0080-\u024F]{1}[a-zA-Z\u0080-\u024F\. |\-| |']*[a-zA-Z\u0080-\u024F\.']{1})$

- 1
- 2
I use that one:
^[a-zA-Z\\u0080-\\u024F.]+((?:[ -.|'])[a-zA-Z\\u0080-\\u024F]+)*$

- 756
- 1
- 9
- 16
Here are some fun edge-cases:
- 's Graveland
- 's Gravendeel
- 's Gravenpolder
- 's Gravenzande
- 's Heer Arendskerke
- 's Heerenberg
- 's Heerenhoek
- 's Hertogenbosch
- 't Harde
- 't Veld
- 't Zand
- 100 Mile House
- 6 October City
So, don't forget to add '
and 0-9
as a possible first character of the city name.

- 3,856
- 2
- 22
- 37