php - about regex, you find {else} and allow nested -


i created pattern: {if ([^}]*)}((?:[\w\s]+[^}]*)){\/if}

but have done long time ago now, pattern is, example:

{if $module=admin}  {/if} 

but want find:

{if $module=admin}  {else}  {/if} 

as far possible find both same pattern.

any ideas? thank :)

/{if ([^}]+)}([\w\w]*?)(?:{else}([\w\w]*?))?{\/if}/g

  • {if literal text.
  • ([^}]+) if condition: 1 or more non-closing-curly-brace (}) characters, captured.
  • } literal text.
  • ([\w\w]*?) inside if: 0 or more characters. match until next part found. (very inefficient; can rewrite make more performant.)
  • (?:{else}([\w\w]*?))? optional group.
    • {else} else: literal text.
    • ([\w\w]*?) inside else: 0 or more characters. match until next part found. (very inefficient; can rewrite make more performant.)
  • {\/if} closing: literal text.

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -