Regex for location matching - Python

Viewed 297

How would you match locations (Places) with regular expressions in python. It should match locations of the following format:

  • London, ENG, United Kingdom
  • Melbourne, VIC, Australia
  • Palo Alto, CA USA

I've tried this but it doesnt work:

re.findall(r'([A-Z][a-z]+ ([A-Z][a-z]+)?,)+',x)

EDIT:

okay, let me make it clear what i want. i have a huge wall of text. i need to detect locations like the above mentioned from the text. not validate.

Example:

text = """
..............................
..............................
London, ENG, United Kingdom...
..............................
"""
re.findall(r'<something>',x)
#['London, ENG, United Kingdom']

it should be able to match any location of the format Xxxx, XXX, Xxxx with optional commas and optionally multiple words

3 Answers
Related