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

487
Vistas
Regex - Do not capture word if it contains another word

I'm currently fighting with regex to achieve something and can't success by myself ...

Here my string: path/to/folder/@here
What I want is something like this: a:path/a:to/a:folder/@here

So my regex is the following one : /([^\/]+)/g, but the problem is that the result will be (preg_replace('/([^\/@]+)/', 'a:$0'): a:path/a:to/a:folder/a:@here ...

How can I had skip the replace if the captured group contains @ ?

I tried this one, without any success : /((?!@)[^\/]+)/g

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

0

Another option could be to match what you want to avoid, and use a SKIP FAIL approach.

/@[^/@]*(*SKIP)(*F)|[^/]+
  • /@ Match literally
  • [^/@]*(*SKIP)(*F) Optionally match any char except / and @ and then skip this match
  • | Or
  • [^/]+ Match 1+ occurrences of any char except /

See a regex demo and a PHP demo.

For example

$str = 'path/to/folder/@here';
echo preg_replace('#/@[^/@]*(*SKIP)(*F)|[^/]+#', 'a:$0', $str);

Output

a:path/a:to/a:folder/@here
over 4 years ago · Santiago Trujillo Denunciar

0

You can use

(?<![^\/])[^\/@][^\/]*

See the regex demo. Details:

  • (?<![^\/]) - a negative lookbehind that requires either start of string or a / char to appear immediately to the left of the current location
  • [^\/@] - a char other than / and @
  • [^\/]* - zero or more chars other than /.

See the PHP demo:

$text = 'path/to/folder/@here';
echo preg_replace('~(?<![^/])[^/@][^/]*~', 'a:$0', $text);
// => a:path/a:to/a:folder/@here
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