Reputation: 2060
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(]
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.
Upvotes: 5
Views: 296
Reputation: 18515
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.
Or without any flags and used [^\n]
instead of \N
for compatibility.
(?<![^\n])(?:(?!\/\/)[^\r\n])*?while[^\r\n]+[\s=(]each[\s(](?!(?:(?!\/\*)[\S\s])*?\*\/)
Upvotes: 2