相关疑难解决方法(0)

正则表达式匹配{if cond} foo {else} bar {/ if}

我很难丢掉我不想要的表达式,并保留我所做的位.

问题是 - 给定输入字符串:

{if cond}foo{else}bar{/if}
Run Code Online (Sandbox Code Playgroud)

我想要:

0: {if cond}foo{else}bar{/if}
1: cond
2: foo
3: bar
Run Code Online (Sandbox Code Playgroud)

并为输入字符串:

{if cond}foo{/if}
Run Code Online (Sandbox Code Playgroud)

我想要:

0: {if cond}foo{else}bar{/if}
1: cond
2: foo
3: 
Run Code Online (Sandbox Code Playgroud)

我目前得到的正则表达式如下:

\{if ([a-z0-9]+)\}([^\{]*?)(((?:\{else\})?)(.*?)?)\{/if\}
Run Code Online (Sandbox Code Playgroud)

我收到以下数据:

0: {if cond}foo{else}bar{/if}
1: cond
2:
3: foo{else}bar
4:
5: foo{else}bar
Run Code Online (Sandbox Code Playgroud)

这将需要进一步解析该foo{else}bar位以获得相应的值.

我的正则表达式是否近在咫尺?

我不太关心可能会运行的数据类型 - 包括{在if语句的主体中允许打破正则表达式.值的foobar可以是任何不包括a的值{.

谢谢,

大教堂

c++ regex boost smarty

0
推荐指数
1
解决办法
3064
查看次数

标签 统计

boost ×1

c++ ×1

regex ×1

smarty ×1