Paul
Paul

Reputation: 7187

regex issue: everything but a complex string

I am building a very basic template engine. My template is super simple:

...html code before...
{{ foreach apples }}
... html code to be repeated {{apple}} ...
{{ endforeach }}

{{ foreach oranges }}
... html code to be repeated {{orange}} ...
{{ endforeach }}
...html code after ...

My goal is to get the first foreach (apples) and I've arrived here: https://regex101.com/r/cD5gY4/2 enter image description here

Does anybody have an idea about how I could stop to the end of the first loop instead of capturing both?

Upvotes: 0

Views: 45

Answers (1)

Pruthvi Raj
Pruthvi Raj

Reputation: 3036

.* is greedy, make it non-greedy by adding a quantifier like ? to it. Try this:

{{\s*foreach ([a-z]+)\s*}}(.*?)({{\s*endforeach\s*}})

REGEX101

enter image description here

Upvotes: 1

Related Questions