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

313
Visualizações
Match "[abc]", "[[abc]", "[abc]]", but not "[[abc]]",

I'm trying to match [abc] unless it's "escaped" by [] from both sides (so [[abc]] is considered as escaped, but not [[abc] or [abc]]).

Closest thing I could find is (?<!\[)\[abc\](?!\]) from Match "ABC" from *ABC*, but not from **ABC**, but it ignores match if it's escaped from only one side.

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

0

The lookahead could be either at the left, or at the right to allow a single backet on the left or right, but not a double square bracket on the other side.

(?<!\[)\[abc]|\[abc](?!])

Regex demo

over 4 years ago · Santiago Trujillo Relatório

0

@Thefourthbird's answer works, but would require that the main pattern abc be repeated, which goes against the DRY principle that most are encouraged to follow.

In the interest of maximizing code reuse, one approach would be to use a capture group to capture [abc] and then use it in a negative lookbehind pattern to ensure that it is not both preceded by a [ and followed by a ]:

(\[abc])(?<!\[\1(?=]))

Demo

Note that this works for C# because .NET happens to support variable-length lookbehind patterns, which aren't supported by many other regex engines.

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