RegEx - Exclude Commented PHP Code Block

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

RegEx - Exclude Commented PHP Code Block



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


while...each


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


^(?:(?!//).)*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.





You could modify your existing expression, use alternation and only capture what is not matched? See here
– UnbearableLightness
5 hours ago





@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
– CM 웃
5 hours ago


while...each




1 Answer
1



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


(*SKIP)(*FAIL)


(?:(?<!:)//.*|/*[sS]*?*/)(*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(](?!(?:(?!/*)[Ss])*?*/)



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


m


s



Another demo at regex101



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


[^n]


N


(?<![^n])(?:(?!//)[^rn])*?while[^rn]+[s=(]each[s(](?!(?:(?!/*)[Ss])*?*/)



One more demo at regex101





Thanks. The tool doesn't seem to support that. Any other way?
– CM 웃
1 hour ago





Maybe the inline modifier, can you use updated regex like this?
– bobble bubble
1 hour ago





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?
– CM 웃
1 hour ago






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 웃
44 mins ago


m





@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])(?:(?!//)[^rn])*while[^rn]+[s=(]each[s(](?!(?:(?!/*)[Ss])*?*/). Great you got it going however :) and happy you like my nickname of course (:
– bobble bubble
39 mins ago



m


^


C#


(?<![^n])(?:(?!//)[^rn])*while[^rn]+[s=(]each[s(](?!(?:(?!/*)[Ss])*?*/)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

Executable numpy error

PySpark count values by condition

Mass disable jenkins jobs