Regex Pattern with two equal but unknown parts

I am currently working on a simple template engine. In a template, if-statements can be used. An if block looks like this

{% name IF: a EQUALS b %}


{% name ENDIF %}

I want to identify these blocks via regex.
The Problem is I need a regex pattern which contains two unknown but equal parts.
This is the pattern which matches to all blocks:

/{% +(.*) +IF: +(.*) +%}([\s\S]*){% +(.*) +ENDIF +%}/gm

To make it clarify which ENDIF marker belongs to which IF the first and the last capture group needs to be the same.
Is there a way to do this?

>Solution :

You may use this regex:


RegEx Demo

RegEx Details:

  • {%: Match {%
  • \s+: Match 1+ whitespaces
  • (\S+):
  • \s+: Match 1+ whitespaces
  • IF:: Match IF:
  • [^}]+: Match 1+ of any character that is not a }
  • %}: Match %}
  • (?s): Enable DOTALL mode so that dot matches line break
  • (.+?): 1st capture group to match 1+ of any characters
  • {%: Match {%
  • \s+: Match 1+ whitespaces
  • \1: Match same value as what we captured in capture group #1
  • \s+: Match 1+ whitespaces
  • ENDIF: Match ENDIF
  • \s+: Match 1+ whitespaces
  • %}: Match closing %}

Leave a Reply