How to match--but not capture--in Python regular expressions?

前端 未结 3 725
被撕碎了的回忆
被撕碎了的回忆 2020-12-31 07:14

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

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-31 07:49

    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).

提交回复
热议问题