re.findall not returning full match?

前端 未结 5 1149
灰色年华
灰色年华 2020-11-28 10:29

I have a file that includes a bunch of strings like \"size=XXX;\". I am trying python\'s re module for the first time and am a bit mystified by the following behavior: if I

5条回答
  •  清酒与你
    2020-11-28 11:09

    In some cases, the non-capturing group is not appropriate, for example with regex which detects repeated words (example from python docs)

    r'(\b\w+)\s+\1'
    

    In this situation to get whole match one can use

    [groups[0] for groups in re.findall(r'((\b\w+)\s+\2)', text)]
    

    Note that \1 has changed to \2.

提交回复
热议问题