Regex, select closest match

后端 未结 3 594
迷失自我
迷失自我 2020-11-29 11:15

Assume the following word sequence

BLA text text text  text text text BLA text text text text LOOK text text text BLA text text BLA

What I

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 11:51

    Another way to extract the desired text is to use the tempered greedy token technique, which matches a series of individual characters that do not begin an unwanted string.

    r'\bBLA\b(?:(?!\bBLA\b).)*\bLOOK\b'
    

    Start your engine! | Python code

    \bBLA\b        : match 'BLA' with word boundaries
    (?:            : begin non-capture group
      (?!\bBLA\b)  : negative lookahead asserts following characters are not
                     'BLA' with word boundaries
      .            : match any character
    )              : end non-capture group
    *              : execute non-capture group 0+ times
    \bLOOK\b       : match 'LOOK' with word boundaries
    

    Word boundaries are included to avoid matching words such as BLACK and TRAILBLAZER.

提交回复
热议问题