开发者

Templating parser

I'd like to create a parser which substitutes text like the following (similar to MediaWiki's syntax):

some text {{template|parameter1|parameter2}} some text

The regex should match the text between the curly braces (split to groups for template name and parameter). This is what I've already got.

Where I don't get further is infinitely nested templates:

some text {{template|{{subtemplate|st-parameter}}|parameter2}} some text

The text should be replaced from the innermost to the outermost template. I'm not sure how to write a regex that doesn't break at the first closing braces in the exa开发者_运维技巧mple above. Best would be if the regex only matches for the innermost template (without {{ and }} inside). Where to start?


It looks like you're running up against the limitations of regular languages. If you're doing things like full-on recursive embedding, without any easy tricks to tell you where the deepest level of nesting is (like Ingo's suggestion of no braces), you want to use a context-free grammar.


You want to match the innermost thing only, that means the text may not contain braces (but is enclosed in braces):

\{([^{}]*)\}

Your result will be in matcher group 1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜