Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

482
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!