Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 %}

content

{% 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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You may use this regex:

{%\s+(\S+)\s+IF:[^}]+%}(?s)(.+?){%\s+\1\s+ENDIF\s+%}

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 %}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading