【发布时间】:2016-04-15 21:06:43
【问题描述】:
我有以下正则表达式:
/{%\h?if ((?:@)?(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(?:\.(?:[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*))*)\h?%}([\s\S]*){%\h?end if\h?%}/U
基本符合以下几点:
{% if variable %}
whatever stuff
{% end if %}
我正在尝试修改它以匹配上面的内容或这样的内容:
{% if variable %}
some stuff
{% else %}
whatever stuff
{% end if %}
之后我想能够说if else_capture_group is not empty, then do something。
我尝试查看负前瞻:
{%\h?if ((?:@)?(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(?:\.(?:[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*))*)\h?%}([\s\S](?!{% else %}))(?:{%\h?else\h?%}([\s\S]*))?{%\h?end if\h?%}
但根据:https://regex101.com/r/wP4vS9/1,它不起作用......我不知道为什么。有人能指出我正确的方向吗?
【问题讨论】: