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

488
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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