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

131
Vistas
Using lookahead, how to ensure at least 4 alphanumeric chars are included + underscores

I'm trying to make sure that at least 4 alphanumeric characters are included in the input, and that underscores are also allowed.

The regular-expressions tutorial is a bit over my head because it talks about assertions and success/failure if there is a match.

^\w*(?=[a-zA-Z0-9]{4})$

my understanding:

\w --> alphanumeric + underscore

* --> matches the previous token between zero and unlimited times ( so, this means it can be any character that is alphanumeric/underscore, correct?)

(?=[a-zA-Z0-9]{4}) --> looks ahead of the previous characters, and if they include at least 4 alphanumeric characters, then I'm good.

Obviously I'm wrong on this, because regex101 is showing me no matches.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You want 4 or more alphanumeric characters, surround by any number of underscores (use ^ and $ to ensure it match's the whole input ):

^(_*[a-zA-Z0-9]_*){4,}$
about 4 years ago · Juan Pablo Isaza Denunciar

0

I suggest using atomic groups (?>...), please see regex tutorial for details

 ^(?>_*[a-zA-Z0-9]_*){4,}$

to ensure 4 or more fragments each of them containing letter or digit.

Edit: If regex doesn't support atomic, let's try use just groups:

  ^(?:_*[A-Za-z0-9]_*){4,}$
about 4 years ago · Juan Pablo Isaza Denunciar

0

Your pattern ^\w*(?=[a-zA-Z0-9]{4})$ does not match because:

  • ^\w* Matches optional word characters from the start of the string, and if there are only word chars it will match until the end of the string
  • (?=[a-zA-Z0-9]{4}) The positive lookahead is true, if it can assert 4 consecutive alphanumeric chars to the right from the current position. The \w* allows backtracking, and can backtrack 4 positions so that the assertion it true.
  • But the $ asserts the end of the string, which it can not match as the position moved 4 steps to the left to fulfill the previous positive lookahead assertion.

Using the lookahead, what you can do is assert 4 alphanumeric chars preceded by optional underscores.

If the assertion is true, match 1 or more word characters.

^(?=(?:_*[a-zA-Z0-9]){4})\w+$

The pattern matches:

  • ^ Start of string
  • (?= Positive lookahead, asser what is to the right is
    • (?:_*[a-zA-Z0-9]){4} Repeat 4 times matching optional _ followed by an alphanumeric char
  • ) Close the lookahead
  • \w+ Match 1+ word characters (which includes the _)
  • $ End of string

Regex demo

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