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

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

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 Report

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