regexcity

Regular Expressions for City name


I need a regular Expression for Validating City textBox, the city textbox field accepts only Letters, spaces and dashes(-).


Solution

  • 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