Background: I'm just fiddling around with an idea for simple templating which only provides if/for/render, to see how feasible it is and if it makes sense to use in my personal project. As opposed to using NVelocity or Razor, or anything else.
I've written a regular expression:
(?:(?:(?<open>\[if (?<if>[a-zA-Z0-9\.]+)\])(?<content>[^\[]*))+(?:[^\[]*(?<close-open>\[end if\]))+)+(?(open)(?!))
And when used with the sample text:
<div>
[if variable3]{{variable3}}[end if]
</div>
<div>
[if variable1]
{{variable1}}
[if variable2]
<br>
{{variable2}}
[end if]
[end if]
</div>
It's working as expected. I get 2 matches, and if the 2nd match is valid I can parse the inner capture.
Problem is if i have multiple nested matches. So given:
<div>
[if variable3]{{variable3}}[end if]
</div>
<div>
[if variable1]
{{variable1}}
[if variable2]
<br>
{{variable2}}
[end if]
[if variable4]
<br>
{{variable4}}
[end if]
[if variable5]
<br>
{{variable5}}
[end if]
[end if]
</div>
What I end up with is the first capture being correct, and then all 3 individual captures and not the outer one for the 2nd match.
If I expand the capture to ignore \[
for the inner content, it causes the first and second match to combine into a single match. :(
Does anyone know how to fix this? (and if you have a better idea of how to do this templating would be keen to know in the comments)