Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

174
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda