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

256
Vistas
regex skip match if its follows by whitespace and a keyword

Currently trying to match comments with regexes but only if no function follows. Currently I use a regex which also matches the keyword function. And then check in the source code (php) if this group is set or not.

/\/\*\*.*?\*\/\s*(function)?/sg

https://regex101.com/r/l0j1ip/1

Now the question is whether it is possible to realize with pure regex. I have tried it with a simple negative lookahead but without success. Although the comment is no longer made individually, but then just with the subsequent comment.

/\/\*\*.*?\*\/\s*(?!function)/sg

https://regex101.com/r/PuUUw6/1

Next I tried non capture group. But also there without success.

/(?:\/\*\*.*?\*\/\s*function)|\/\*\*.*?\*\/\s*/sg

https://regex101.com/r/wkQE7E/1

After a comment with the information (*SKIP)(*FAIL) I also tried it without success. All matches above this keyword are skipped. Also the single matches are skipped.

/\/\*\*.*?\*\/\s*function(*SKIP)(*FAIL)|\/\*\*.*?\*\//sg

https://regex101.com/r/OJSFrF/1

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

After reading the question again, it should be doable using negative lookahead ; the repetition must be inside the negative expression:

/\/\*\*((?!\*\/).)*\*\/(?!\s*function)/sg

Seems you need to understand better how backtracking works, using .*? instead of .* means the regex engine will try first to match everything after before .* however the negative lookahead makes the match fail and .* continues to match. Using ((?!\*\/).)* can't match \*\/ wheras .*? can, after backtracking. Another solution is to use atomic group (?>\/\*\*.*?\*\/)(?!\s*function).

over 4 years ago · Santiago Trujillo Denunciar

0

Another option without the /s flag could be

/\*\*(?:[^*]*+|\*(?!/)[^*]*+)*\*/(?!\s*function)

The pattern matches:

  • /\*\* Match /**
  • (?: Non capture group
    • [^*]*+ Match any char except * using a possessive quantifier
    • | Or
    • \*(?!/) Match * not followed by /
    • [^*]*+ Match any char except * using a possessive quantifier
  • )* Close non capture group and optionally repeat
  • \*/ Match */
  • (?!\s*function) Negative lookahead, assert not optional whitspace chars followed by function to the right

Regex demo

Note that you don't have to escape the backslash when using a different delimiter.

$regex = '~/\*\*(?:[^*]*+|\*(?!/)[^*]*+)*\*/(?!\s*function)~';
over 4 years ago · Santiago Trujillo 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