Regular expression: match everything but the full token

I have the following snippet where I would like to extract the code between {foreach}

and {/foreach}

using a regex:

{foreach (...)}
Some random HTML content <div class="">aklakdls</div> and some {$/r/template} markup inside.
{/foreach}

      

I already have it:

{foreach [^}]*}

      

but after that I can't do anything about it. Is there a way to match something to BUT {/ foreach} in general? Note that the content between {foreach} {/ foreach} can also contain "{$" tokens.

Edit : BaileyP and Tomalak's answers are correct, but I chose BaileyP's answer for simplicity.

+1


source to share


2 answers


I came up with this

/(?:{foreach .*?})(.*?)(?:{\/foreach})/gis

      



Tested with RegExr

+5


source


If your regex flavor didn't support the unwanted match, the following would do it, but as I recommend @ BaileyP's answer .

\{foreach [^}]*\}((?:.(?!\{/foreach\}))*[^{]?)

      

Negative zero-width and non-capturing groups look slightly different depending on your regex.



Here are the components:

\ {foreach [^}] * \} // pretty much self-explanatory
(// match group one starts (that what you are looking for)
 (?: // non-capturing group starts
  ... // anything ...
  (?! \ {/ foreach \}) // ... that is not followed by "{/ foreach}"
 ) * // non-capturing group ends, repeat as often as possible
 [^ {]? // match the last character, unless it is "{"
) // match group one ends, done
+2


source







All Articles