Reputation: 4568
In my .NET app, I am required to parse text, which can have inline conditions, like this:
Here is some text. {{if: condition }} Here is some conditional text. {{endif}} Here is more text.
And so I have written the following regular expression to find these conditions:
\{\{if\:(?<condition>[^\}]+)\}\}(?<value>.+)\{\{endif\}\}
This has worked fine for me, and achieved what I want, until I have had to deal with an input with two conditions:
{{if: condition }} content {{endif}} some other content {{if: condition2 }} content2 {{endif}}
In this case, my regular expression picks up the entire string, starting with the {{if}} of the first condition, and ending with the {{endif}} of the second condition, making my applciation not work correctly.
How can I rewrite my regular expression to make this work? Or do I have to achieve it without regular expressions?
EDIT: I should have said the content within the conditions can also have double curly brackets to represent other constructs, and so it's not as simple is just ignoring those!
NOTE: There is also the potential issue of nested conditions, but I don't think I'll have to worry about those!
Upvotes: 1
Views: 86
Reputation: 60190
Your problem is the greedy quantifier for the value
group. Use this:
\{\{if\:(?<condition>[^\}]+)\}\}(?<value>.+?)\{\{endif\}\}
Upvotes: 2