Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
How would I match nested "if" statements in regex? (using ruby)

I'm using ruby. I'm looking to match the outer "if" group of this:

{#if x > 10}
    <p>{x} is greater than 10</p>
    {#if 5 > x}
        <p>{x} is less than 5</p>
    {/if}
{/if}

What I have so far is this regex:

\{#if(.*)}(.|\n)*?\{\/if\}

But that just matches the first /if statement instead of the last one (see https://rubular.com/r/4l89GlANV9AVCQ). Any ideas?

(I tried this recursive regex matcher I saw on Stack Overflow: https://rubular.com/r/7a4PYTtxFQftob, but I'm struggling to make it work for the above)

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

It's basically the same as the regex for balanced parentheses that you linked to, but since the markers here are of multiple characters rather than single characters, you would need to use a negative lookahead pattern instead of simply a negated character class to ensure a matching character in the middle isn't part of the markers for the balanced construct:

{#if.*?}(?>(?!{(?:#if.*?|\/if)}).|\g<0>)*{\/if}

Demo: https://rubular.com/r/7BU2FStPEP34W4

Note that you should enable MULTILINE mode with the m option in order for . to match a newline character.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda