Regex with negative lookahead across multiple lines

前端 未结 2 1416
攒了一身酷
攒了一身酷 2020-12-19 11:38

For the past few hours I\'ve been trying to match address(es) from the following sample data and I can\'t get it to work:

medicalHistory      None
address            


        
2条回答
  •  春和景丽
    2020-12-19 11:51

    The problem with your regex is that + is greedy and goes until it finds a character out of that group, the @ in the first case and - in the second.

    Another approach is to use a non-greedy quantifier and a positive look-ahead for a newline followed by a word-character, like (python version):

    re.findall(r'address\s+.*?(?=\n\w)', s, re.DOTALL)
    

    It yields:

    ['address             24 Lewin Street, KUBURA, \n                NSW, Australia',
     'address             16 Yarra Street, \n                                     LAWRENCE, VIC, Australia']
    

提交回复
热议问题