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

149
Visualizações
Use lookbehind to ignore a string between two matches, but capture it anyway

I want to match instances of 'the blah blah noun' throughout a text, where the blah blah can include many words, but NOT 'the'.

This regex:

/(\b([Tt]he|[Aa]n|[Aa]|[Aa]nother)\b)?((?!\b(the|an?|another)\b).)*\b(cat|apple)\b/g

Produces these matches: The small cat sat on a huge black cat and ate an apple.

I tried some regex here: https://regexr.com/674l6

The matching is working, but I can't capture complete matches. When you hover over the matches in regexr, not all capture groups are available. I'm only able to capture the beginning and end of the matched string.

There's a basic demo here.

I've been tweaking the regex for hours but to no avail.

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

0

For the characters in the middle, you only have a match for the last single char in a capture group, as you are repeating the capture group.

You can add a capture group around the whole that you are matching, and turn the repeating capture group in a non capture group.

As you want to match the blah blah noun the first capture group should not be optional.

The match now has 3 capture groups instead of 5.

\b([Tt]he|[Aa]n(?:other)?|[Aa])\b((?:(?!\b(?:the|an?|another)\b).)*)\b(cat|apple)\b

The updated pattern in parts:

  • \b A word boundary to prevent a partial match
  • ( Capture group 1
    • [Tt]he|[Aa]n(?:other)?|[Aa] Match one of the alternatives
  • ) Close group 1
  • \b A word boundary
  • ( Capture group 2
    • (?:(?!\b(?:the|an?|another)\b).)* Repeat matching any char not directly followed by any of the alternatives (tempered greedy token)
  • ) Close group 2
  • \b(cat|apple)\b Capture group 3, match either cat or apple between word boundaries

Regex demo

See a non greedy pattern version to not capture the surrounding spaces in group 2.

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