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

前端 未结 3 724
被撕碎了的回忆
被撕碎了的回忆 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:38

    matches = re.search(r'(\w+\s)(?:D\.C\.)(, \w\w)(?=\W)', location).group(1,2)
    match = ''.join(matches)
    

    When it says it is "non-capturing", that means it won't make a separate captured group for it. The text "D.C." is still in the match. See http://docs.python.org/library/re.html#match-objects

提交回复
热议问题