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

118
Visualizações
regex capture content of {} only if no part is inside "" (and other encapsulators)

are regular expressions even appropriate for this problem? I'm trying to extract blocks of text surrounded by 'raw' curly braces. so anything surrounded by "" or **,"",~~,``, should be ignored entirely. sample text:

{
"text1"
'text2'
*text3*
~text4~
`text5`
}
{
"half  broken1}{"
*half  broken2}{*
'half  broken3}{'
~half  broken4}{~
`half  broken5}{`
}
{
"{nested text1}"
'{nested text2}'
~{nested text3}~
*{nested text4}*
`{nested text5}`
}
{
"text1"
{
`text2`
}
}

expected output:

[
"\"text1\"
'text2'
*text3*
~text4~
`text5`",

"\"half  broken1}{\"
*half  broken2}{*
'half  broken3}{'
~half  broken4}{~
`half  broken5}{`",

"\"{nested text1}\"
'{nested text2}'
~{nested text3}~
*{nested text4}*
`{nested text5}`",

"\"text1\"
{
`text2`
}",
"`text2`"
]

I can target curly braces with:

/(?<=\{)[^}]*(?=\})/gm

but that eats the first } facing brace it finds to match. I have tried a few other strings, but so far - I don't know how to ignore {"}"} the first brace in this case.

(using js syntax for regex)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

https://regex101.com/r/EODDXL/1

(?<=\{)(?:"[^"]*"|'[^']*'|\*[^*]*\*|~[^~]*~|`[^`]*`|{[^}]*}|[^}])*?(?=\})

That's starting with what you had, but includes ignoring " followed by 0 or more [^"] followed by a ". Then repeat this pattern for ', *, ~, ` and {}. This captures the outer groups only. I'm not sure how exactly you'd get the expected output at the end, but this may get you in the right direction.

about 4 years ago · Juan Pablo Isaza 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