Variable length look-behind

后端 未结 5 1155
猫巷女王i
猫巷女王i 2020-12-16 13:38

Is there any elegant solution to build a variable length look-behind regex such as this one ?

/(?<=eat_(apple|pear|orange)_)today|yesterday/g;

5条回答
  •  渐次进展
    2020-12-16 13:58

    Blog post found today, linked to me at #regex @ irc.freenode.org:

    http://www.drregex.com/2019/02/variable-length-lookbehinds-actually.html

    This article explains how to do a variable width look-behind in PCRE.

    The solution would then be:

    /(?=(?=(?'a'[\s\S]*))(?'b'eat_(?:apple|pear|orange)_(?=\k'a'\z)|(?<=(?=x^|(?&b))[\s\S])))today|yesterday/g

    https://regex101.com/r/9DNpFj/1

提交回复
热议问题