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

173
Visualizações
Solve Catastrophic Backtracking in my regex

I am trying to detect names (French names) that can be in the following formats

CHARLES

CHARLES-ANTOINE

D'URENBURRY

CHARLES--ANTOINE

CHARLES ANTOINE

And for that I have the following regex

/^([A-Z]+(([-]{0,2}|[']|[ ])?[A-Z]+))+$/

Which works, but GitHub's code scanner shows this error

This part of the regular expression may cause exponential backtracking on strings containing many repetitions of 'AA'.

I understand the error, however, I'm not sure how to solve it.

My question is: how to solve it.

Thank you

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

0

You need to re-write the pattern like

^[A-Z]+(?:(?:-{1,2}|[' ])[A-Z]+)*$

See the regex demo. Now, each subsequent pattern will match different string, at different locations inside the string.

Details:

  • ^ - start of string
  • [A-Z]+ - one or more uppercase ASCII letters
  • (?:(?:-{1,2}|[' ])[A-Z]+)* - zero or more repetions of:
    • (?:-{1,2}|[' ]) - a non-capturing group matching one or two hyphens, a space or apostrophe
    • [A-Z]+ - one or more uppercase ASCII letters
  • $ - end of string.
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