wiki138
wiki138

Reputation: 1

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 example above. Best would be if the regex only matches for the innermost template (without {{ and }} inside). Where to start?

Upvotes: 0

Views: 114

Answers (2)

Rachel Shallit
Rachel Shallit

Reputation: 2020

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.

Upvotes: 2

Ingo
Ingo

Reputation: 36349

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.

Upvotes: 0

Related Questions