普通防爆pression匹配{如果COND} {foo的其他} {酒吧/如果} [英] Regular Expression to match {if cond}foo{else}bar{/if}
问题描述
我有困难,丢掉前pression我不希望的位,并保持位我做的。
I'm having difficulty throwing away the bits of the expression I don't want, and keeping the bits I do.
问题是 - 定的输入字符串:
The problem is - given the input string:
{if cond}foo{else}bar{/if}
我想正好有:
0: {if cond}foo{else}bar{/if}
1: cond
2: foo
3: bar
而对于输入字符串:
And for the input string:
{if cond}foo{/if}
我想正好有:
0: {if cond}foo{else}bar{/if}
1: cond
2: foo
3:
我在present得到的正则表达式如下:
The regex I've got at present looks like this:
\{if ([a-z0-9]+)\}([^\{]*?)(((?:\{else\})?)(.*?)?)\{/if\}
我得到以下数据传回:
I get the following data back:
0: {if cond}foo{else}bar{/if}
1: cond
2:
3: foo{else}bar
4:
5: foo{else}bar
这将要求在 foo的进一步解析{}其他酒吧
位来获取各自的值。
是我正则表达式的任何地方附近接近?
Is my regex anywhere near close?
我不是太在意数据的排序,这可能会跑过来 - 包括 {
在if语句中的主体被允许打破正则表达式。 富
和栏的值
可以是任何不包括 {
。
I'm not too concerned about the sort of data this might be run over - including {
in the body of an if statement is allowed to break the regex. The values of foo
and bar
can be anything that does not include a {
.
谢谢,
大教堂
推荐答案
这个是什么?
\{if ([a-z0-9]+)\}([^\{]*)(?:\{else\})?([^\{]*)\{/if\}
这篇关于普通防爆pression匹配{如果COND} {foo的其他} {酒吧/如果}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!