5

I am using a file search utility (FileSeek) with regex content search. The contents I am searching for is basically any un-commented lines that have while...each in them. I have successfully managed to exclude inline commented lines such as // while (list($key, $value) = each($_GET)) with this regex: ^(?:(?!\/\/).)*while.+[\s=(]each[\s(]

Demo

How can I improve the regex search (make it even more restrictive) to exclude search results from commented lines and commented code blocks \* *\ such as:

/*
  while (list($key, $value) = each($_GET))
*/

Or

/* some code
  while (list($key, $value) = each($_GET))
  some code
*/

In other words, how can I modify my regex to also completely skip/ignore everything inside a commented php block: \* *\ instead of picking up results that are also inside it?

EDIT: Just for reference, here is an example that does the opposite, ie. matches only commented code.

2
  • You could modify your existing expression, use alternation and only capture what is not matched? See here Commented Jul 29, 2018 at 9:29
  • 1
    @UnbearableLightness Thanks. I am looking for a solution that is more restrictive than the regex I already posted in the question, which currently picks the while...each in the commented code block: regex101.com/r/pCQ3QC/1 Commented Jul 29, 2018 at 9:43

1 Answer 1

4

You can use (*SKIP)(*FAIL) to skip parts together with this trick if supported by your tool.

(?:(?<!:)\/\/.*|\/\*[\s\S]*?\*\/)(*SKIP)(*F)|while.+?[\s=(]each[\s(]

See demo at regex101. This is just a quick try, you need to adjust the pattern to your needs.


If this is not supported by your tool, you can try to add another lookahead to your pattern.

^(?:(?!\/\/).)*while.+[\s=(]each[\s(](?!(?:(?!\/\*)[\S\s])*?\*\/)

With m multiline-mode turned on and s single line mode turned off.

Another demo at regex101


Or without any flags and used [^\n] instead of \N for compatibility.

(?<![^\n])(?:(?!\/\/)[^\r\n])*?while[^\r\n]+[\s=(]each[\s(](?!(?:(?!\/\*)[\S\s])*?\*\/)

One more demo at regex101

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks. The tool doesn't seem to support that. Any other way?
Maybe the inline modifier, can you use updated regex like this?
When I am trying the exact regex you put in your demos, it says "RegEx pattern is invalid" unfortunately. It does work well in other engines tho, but not this tool. Probably doesn't accept SKIP, F. Any alternatives?
Well, it seems like FileSeek doesn't match m mulltiline, but this tool sourceforge.net/projects/grepwin does and seems to work beautifully with your regex, so thanks again!
@CM웃 welcome, I thought of m because your pattern uses ^ for line start. Fileseek seems to use C# regex flavor. Without any flags the last idea I could think of would be (?<![^\n])(?:(?!\/\/)[^\r\n])*while[^\r\n]+[\s=(]each[\s(](?!(?:(?!\/\*)[\S\s])*?\*\/). Great you got it going however :) and happy you like my nickname of course (:
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.