Why doesn't finite repetition in lookbehind work in some flavors?

前端 未结 4 1236
臣服心动
臣服心动 2020-12-10 21:11

I want to parse the 2 digits in the middle from a date in dd/mm/yy format but also allowing single digits for day and month.

This is what I came up with

4条回答
  •  醉梦人生
    2020-12-10 21:57

    To quote regular-expressions.info:

    The bad news is that most regex flavors do not allow you to use just any regex inside a lookbehind, because they cannot apply a regular expression backwards. Therefore, the regular expression engine needs to be able to figure out how many steps to step back before checking the lookbehind.

    Therefore, many regex flavors, including those used by Perl and Python, only allow fixed-length strings. You can use any regex of which the length of the match can be predetermined. This means you can use literal text and character classes. You cannot use repetition or optional items. You can use alternation, but only if all options in the alternation have the same length.

    In other words your regex does not work because you're using a variable-width expression inside a lookbehind and your regex engine does not support that.

提交回复
热议问题