I\'ve got a function spitting out \"Washington D.C., DC, USA\" as output. I need to capture \"Washington, DC\" for reasons that have to do with how I handle every single oth
I'm late to this and the first two answers were great, but if by any chance you need a general regex for pulling cities out of a combination of cities, suffixes, states/provinces, and countries, but you know D.C. is an annoying special case, you might be able to use the following:
>>> import re
>>> city = re.compile(r'(\w+(?:\sD\.C\.)?), \w\w(?=\W)')
>>> location = "Washington D.C., DC, USA"
>>> re.search(city, location).group(1)
'Washington D.C.'
>>> location = "Vancouver, BC, Canada"
>>> re.search(city, location).group(1)
'Vancouver'
The D.C. part is made optional (as you don't always need it) in addition to being non-capturing (to save memory).