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
That's a clever way indeed, but not-capturing doesn't mean removing it from match. It just mean, that it's not considered as an output group.
You should try to do something similar to the following:
match = re.search(r'(\w+)\s(?:D\.C\.), (\w\w)\W', location).groups()
This prints ('Washington', 'DC')
.
Note the difference between .group()
and .groups()
. The former gives you the whole string that was matched, the latter only the captured groups. Remember, you need to specify what you want to include in the output, not what you want to exclude.