openresty / replace-filter-nginx-module

Streaming regular expression replacement in response bodies
260 stars 68 forks source link

removing C comments creates blank line. #7

Closed bhargavtrivedi closed 10 years ago

bhargavtrivedi commented 11 years ago

Hi,

I tried removing comments using below code which results in blank line. replacefilter "'(?:\[^\n]|[^'\n])'" $& g; replacefilter '"(?:\[^\n]|[^"\n])"' $& g; replacefilter '/*.?_/|//[^\n]*' '' g;

I have also used below code to remove space and blank lines but it works for existing blank lines only. It doesn't remove the balnk lines create by removing some strings etc. replace_filter '^\s+|\s+$' '' g;

Is there any code which can just remove the blank lines (without space)?

One more question, if we use multiple replace_filter code, in which sequence will it be executed?

Thanks, Bhargav

agentzh commented 11 years ago

Hello!

On Mon, Oct 28, 2013 at 6:59 AM, bhargavtrivedi notifications@github.com wrote:

I have also used below code to remove space and blank lines but it works for existing blank lines only. It doesn't remove the balnk lines create by removing some strings etc. replace_filter '^\s+|\s+$' '' g;

Is there any code which can just remove the blank lines (without space)?

I don't quite understand your question. Could you elaborate by giving some sample C++ code (and the expected filtering output)?

One more question, if we use multiple replace_filter code, in which sequence will it be executed?

Logically speaking, all the regexes in the replace_filter directives are executed at the same time (and in a single run), but with different priorities according to their order in nginx.conf.

Best regards, -agentzh

bhargavtrivedi commented 11 years ago

Hi,

I tried to remove comments from backend response in Nginx using below code(I found this in README)

replace_filter "'(?:\\\\[^\n]|[^'\n])*'" $& g;
replace_filter '"(?:\\\\[^\n]|[^"\n])*"' $& g;
replace_filter '/\*.*?\*/|//[^\n]*' '' g;

The above code removes the comment but leaves the blank line in response.

I have also used below code to remove blank lines from backend response. replace_filter '^\s+|\s+$' '' g;

It removes the leading and trailing spaces as well as blank lines from backend response but it doesn't remove the blank lines which are created with removing the comments(using above regex).

Regards, Bhargav

agentzh commented 11 years ago

@bhargavtrivedi You should not assume the substitutions are performed in multiple separate passes, rather, you should design your patterns for one single pass.

So you should remove adjacent spaces along with the comments, that is,

replace_filter '\s*(?:/\*.*?\*/|//[^\n]*)\s*' '' g;

Regards, -agentzh

bhargavtrivedi commented 11 years ago

Hi,

Thanks for your quick response. I will check your given code.

Best regards, Bhargav