You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Mayuresh <ma...@gmail.com> on 2016/11/22 16:50:34 UTC

[users@httpd] Mod_Substitute - Match the last occurrence of a string in the response

Hi,

How can I check for the last occurrence of a string in the response html
and only replace the last occurrence of it?

I want to search for the last "meta" tag in the response and replace it
with something.

How can I do this?

Thanks,
Mayuresh

Re: [users@httpd] Mod_Substitute - Match the last occurrence of a string in the response

Posted by Dr James Smith <js...@sanger.ac.uk>.
Never used mod_substitute - but the standard PCRE way is s/(.*)<meta... 
 >/$1..../mxs - the .* will capture greedily - so captures all but last 
meta...



On 22/11/2016 16:50, Mayuresh wrote:
> Hi,
>
> How can I check for the last occurrence of a string in the response 
> html and only replace the last occurrence of it?
>
> I want to search for the last "meta" tag in the response and replace 
> it with something.
>
> How can I do this?
>
> Thanks,
> Mayuresh




-- 
 The Wellcome Trust Sanger Institute is operated by Genome Research 
 Limited, a charity registered in England with number 1021457 and a 
 company registered in England with number 2742969, whose registered 
 office is 215 Euston Road, London, NW1 2BE. 

Re: [users@httpd] Mod_Substitute - Match the last occurrence of a string in the response

Posted by Mayuresh <ma...@gmail.com>.
Hi,

I am able to modify the response. The problem is it is happening per line.
so with something like this:

Substitute "s|(.*<meta [^>]*>)|$1<script>window['start-time'] = new
Date().getTime();</script><script src='/epicdev.js'></script>|i"


It is doing the replacement for all the meta tags in the html. I just want
it to happen for the last occurrence.


James, I tried your suggestion, but it works for all the lines.

On Tue, Nov 22, 2016 at 9:51 AM, bob gailer <bg...@gmail.com> wrote:

> On 11/22/2016 11:50 AM, Mayuresh wrote:
>
>> Hi,
>>
>> How can I check for the last occurrence of a string in the response html
>> and only replace the last occurrence of it?
>>
>> I want to search for the last "meta" tag in the response and replace it
>> with something.
>>
> Sorry - I just noticed Mod_Substitute in your subject
>
> Check https://serverpilot.io/community/articles/how-to-use-apache-
> to-replace-strings-in-responses.html
>
> It points to http://httpd.apache.org/docs/2.4/mod/mod_substitute.html.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
>
>


-- 
-Mayuresh

Re: [users@httpd] Mod_Substitute - Match the last occurrence of a string in the response

Posted by bob gailer <bg...@gmail.com>.
On 11/22/2016 11:50 AM, Mayuresh wrote:
> Hi,
>
> How can I check for the last occurrence of a string in the response 
> html and only replace the last occurrence of it?
>
> I want to search for the last "meta" tag in the response and replace 
> it with something.
Sorry - I just noticed Mod_Substitute in your subject

Check 
https://serverpilot.io/community/articles/how-to-use-apache-to-replace-strings-in-responses.html

It points to http://httpd.apache.org/docs/2.4/mod/mod_substitute.html.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Mod_Substitute - Match the last occurrence of a string in the response

Posted by Nick Kew <ni...@apache.org>.
On Tue, 2016-11-22 at 11:32 -0800, Mayuresh wrote:
> Is there a way to make a 2 pass substitution? 1st one removing all the
> \n's and then using another one to substitute the string that I want?

How big are your pages?  If not too big, it would be simpler just to
set no line-end and parse as a single line.

(If pages are big it would still be simpler, but a big performance hit).

-- 
Nick Kew



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Mod_Substitute - Match the last occurrence of a string in the response

Posted by Nick Kew <ni...@apache.org>.
On Wed, 2016-11-23 at 09:25 +0000, Dr James Smith wrote:
> Why are you attaching after the last meta tag - wouldn't it be easier
> just before the </head> tag or just after the <head> tag - you should
> have no other js in the header - except possibly an HTML 5 shim...

A </head> is of course optional (implied if not present) in HTML.

If you're parsing markup, mod_proxy_html saves you having to
throw dodgy heuristics at it.

-- 
Nick Kew


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Mod_Substitute - Match the last occurrence of a string in the response

Posted by Dr James Smith <js...@sanger.ac.uk>.
Why are you attaching after the last meta tag - wouldn't it be easier 
just before the </head> tag or just after the <head> tag - you should 
have no other js in the header - except possibly an HTML 5 shim...


On 23/11/2016 08:08, Mayuresh wrote:
>
> Any suggestions?
>
>
> On Nov 22, 2016 11:32 AM, "Mayuresh" <mayuresh.kshirsagar@gmail.com 
> <ma...@gmail.com>> wrote:
>
>     Is there a way to make a 2 pass substitution? 1st one removing all
>     the \n's and then using another one to substitute the string that
>     I want?
>
>     On Tue, Nov 22, 2016 at 11:15 AM, Mayuresh
>     <mayuresh.kshirsagar@gmail.com
>     <ma...@gmail.com>> wrote:
>
>         Hi Jason,
>
>         I tried it:
>
>         Substitute "s%(<meta
>         [^>]*>).*?$%$1<script>window['start-time'] = new
>         Date().getTime();window['app-key'] = \"xxx\";</script><script
>         src='/epicdev.js'></script>%i"
>
>
>         However it still replaces each line that the meta tag appears on.
>
>
>         Regards,
>
>         Mayuresh
>
>
>         On Tue, Nov 22, 2016 at 11:12 AM, Mayuresh
>         <mayuresh.kshirsagar@gmail.com
>         <ma...@gmail.com>> wrote:
>
>             Hi Jason,
>
>             Each meta tag is appearing on a separate line. Will it
>             work even then? Trying it out any ways.
>
>             Thanks,
>             Mayuresh
>
>             On Tue, Nov 22, 2016 at 10:41 AM, Jason Brooks
>             <jason.brooks@eroi.com <ma...@eroi.com>> wrote:
>
>                 Hello,
>
>                 According to the Apache Glossary page
>                 <https://httpd.apache.org/docs/current/glossary.html#regex>,
>                 it\u2019s all PCRE <http://www.pcre.org/> based. So you
>                 should be able to use the \u201cnon-greedy\u201d match.  So
>                 instead of .* which will match all instances, use
>                 .*?.  If you anchor it at the end of the string,
>                 something like (text you are matching against).*?$,
>                 then you can be certain of getting the last one.
>
>                 \u2014jason
>
>                 Jason Brooks 	Systems Administrator
>                 eROI 	Performance is Art.
>
>                 m: 	505 nw couch #300 	w: 	eroi.com <http://eroi.com/>
>                 t: 	503.290.3105 <tel:503.290.3105> 	f: 	503.228.4249
>                 <tel:503.228.4249>
>
>                 	
>                 	fb: 	fb.com/eROI <http://www.facebook.com/eROI>
>
>                 	
>                 	
>                 	
>
>
>
>
>
>>                 On Nov 22, 2016, at 8:50 AM, Mayuresh
>>                 <mayuresh.kshirsagar@gmail.com
>>                 <ma...@gmail.com>> wrote:
>>
>>                 Hi,
>>
>>                 How can I check for the last occurrence of a string
>>                 in the response html and only replace the last
>>                 occurrence of it?
>>
>>                 I want to search for the last "meta" tag in the
>>                 response and replace it with something.
>>
>>                 How can I do this?
>>
>>                 Thanks,
>>                 Mayuresh
>
>
>
>
>             -- 
>             -Mayuresh
>
>
>
>
>         -- 
>         -Mayuresh
>
>
>
>
>     -- 
>     -Mayuresh
>




-- 
 The Wellcome Trust Sanger Institute is operated by Genome Research 
 Limited, a charity registered in England with number 1021457 and a 
 company registered in England with number 2742969, whose registered 
 office is 215 Euston Road, London, NW1 2BE. 

Re: [users@httpd] Mod_Substitute - Match the last occurrence of a string in the response

Posted by Mayuresh <ma...@gmail.com>.
Any suggestions?

On Nov 22, 2016 11:32 AM, "Mayuresh" <ma...@gmail.com> wrote:

> Is there a way to make a 2 pass substitution? 1st one removing all the
> \n's and then using another one to substitute the string that I want?
>
> On Tue, Nov 22, 2016 at 11:15 AM, Mayuresh <ma...@gmail.com>
> wrote:
>
>> Hi Jason,
>>
>> I tried it:
>>
>> Substitute "s%(<meta [^>]*>).*?$%$1<script>window['start-time'] = new
>> Date().getTime();window['app-key'] = \"xxx\";</script><script
>> src='/epicdev.js'></script>%i"
>>
>>
>> However it still replaces each line that the meta tag appears on.
>>
>>
>> Regards,
>>
>> Mayuresh
>>
>> On Tue, Nov 22, 2016 at 11:12 AM, Mayuresh <mayuresh.kshirsagar@gmail.com
>> > wrote:
>>
>>> Hi Jason,
>>>
>>> Each meta tag is appearing on a separate line. Will it work even then?
>>> Trying it out any ways.
>>>
>>> Thanks,
>>> Mayuresh
>>>
>>> On Tue, Nov 22, 2016 at 10:41 AM, Jason Brooks <ja...@eroi.com>
>>> wrote:
>>>
>>>> Hello,
>>>>
>>>> According to the Apache Glossary page
>>>> <https://httpd.apache.org/docs/current/glossary.html#regex>, it’s all
>>>> PCRE <http://www.pcre.org/> based.  So you should be able to use the
>>>> “non-greedy” match.  So instead of .* which will match all instances, use
>>>> .*?.  If you anchor it at the end of the string, something like (text you
>>>> are matching against).*?$, then you can be certain of getting the last one.
>>>>
>>>> —jason
>>>>
>>>> Jason Brooks Systems Administrator
>>>> eROI Performance is Art.
>>>>
>>>> m: 505 nw couch #300 w: eroi.com
>>>> t: 503.290.3105 f: 503.228.4249
>>>>
>>>>
>>>> fb: fb.com/eROI <http://www.facebook.com/eROI>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> On Nov 22, 2016, at 8:50 AM, Mayuresh <ma...@gmail.com>
>>>> wrote:
>>>>
>>>> Hi,
>>>>
>>>> How can I check for the last occurrence of a string in the response
>>>> html and only replace the last occurrence of it?
>>>>
>>>> I want to search for the last "meta" tag in the response and replace it
>>>> with something.
>>>>
>>>> How can I do this?
>>>>
>>>> Thanks,
>>>> Mayuresh
>>>>
>>>>
>>>>
>>>
>>>
>>> --
>>> -Mayuresh
>>>
>>
>>
>>
>> --
>> -Mayuresh
>>
>
>
>
> --
> -Mayuresh
>

Re: [users@httpd] Mod_Substitute - Match the last occurrence of a string in the response

Posted by Mayuresh <ma...@gmail.com>.
Is there a way to make a 2 pass substitution? 1st one removing all the \n's
and then using another one to substitute the string that I want?

On Tue, Nov 22, 2016 at 11:15 AM, Mayuresh <ma...@gmail.com>
wrote:

> Hi Jason,
>
> I tried it:
>
> Substitute "s%(<meta [^>]*>).*?$%$1<script>window['start-time'] = new
> Date().getTime();window['app-key'] = \"xxx\";</script><script
> src='/epicdev.js'></script>%i"
>
>
> However it still replaces each line that the meta tag appears on.
>
>
> Regards,
>
> Mayuresh
>
> On Tue, Nov 22, 2016 at 11:12 AM, Mayuresh <ma...@gmail.com>
> wrote:
>
>> Hi Jason,
>>
>> Each meta tag is appearing on a separate line. Will it work even then?
>> Trying it out any ways.
>>
>> Thanks,
>> Mayuresh
>>
>> On Tue, Nov 22, 2016 at 10:41 AM, Jason Brooks <ja...@eroi.com>
>> wrote:
>>
>>> Hello,
>>>
>>> According to the Apache Glossary page
>>> <https://httpd.apache.org/docs/current/glossary.html#regex>, it’s all
>>> PCRE <http://www.pcre.org/> based.  So you should be able to use the
>>> “non-greedy” match.  So instead of .* which will match all instances, use
>>> .*?.  If you anchor it at the end of the string, something like (text you
>>> are matching against).*?$, then you can be certain of getting the last one.
>>>
>>> —jason
>>>
>>> Jason Brooks Systems Administrator
>>> eROI Performance is Art.
>>>
>>> m: 505 nw couch #300 w: eroi.com
>>> t: 503.290.3105 f: 503.228.4249
>>>
>>>
>>> fb: fb.com/eROI <http://www.facebook.com/eROI>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> On Nov 22, 2016, at 8:50 AM, Mayuresh <ma...@gmail.com>
>>> wrote:
>>>
>>> Hi,
>>>
>>> How can I check for the last occurrence of a string in the response html
>>> and only replace the last occurrence of it?
>>>
>>> I want to search for the last "meta" tag in the response and replace it
>>> with something.
>>>
>>> How can I do this?
>>>
>>> Thanks,
>>> Mayuresh
>>>
>>>
>>>
>>
>>
>> --
>> -Mayuresh
>>
>
>
>
> --
> -Mayuresh
>



-- 
-Mayuresh

Re: [users@httpd] Mod_Substitute - Match the last occurrence of a string in the response

Posted by Mayuresh <ma...@gmail.com>.
Hi Jason,

I tried it:

Substitute "s%(<meta [^>]*>).*?$%$1<script>window['start-time'] = new
Date().getTime();window['app-key'] = \"xxx\";</script><script
src='/epicdev.js'></script>%i"


However it still replaces each line that the meta tag appears on.


Regards,

Mayuresh

On Tue, Nov 22, 2016 at 11:12 AM, Mayuresh <ma...@gmail.com>
wrote:

> Hi Jason,
>
> Each meta tag is appearing on a separate line. Will it work even then?
> Trying it out any ways.
>
> Thanks,
> Mayuresh
>
> On Tue, Nov 22, 2016 at 10:41 AM, Jason Brooks <ja...@eroi.com>
> wrote:
>
>> Hello,
>>
>> According to the Apache Glossary page
>> <https://httpd.apache.org/docs/current/glossary.html#regex>, it’s all
>> PCRE <http://www.pcre.org/> based.  So you should be able to use the
>> “non-greedy” match.  So instead of .* which will match all instances, use
>> .*?.  If you anchor it at the end of the string, something like (text you
>> are matching against).*?$, then you can be certain of getting the last one.
>>
>> —jason
>>
>> Jason Brooks Systems Administrator
>> eROI Performance is Art.
>>
>> m: 505 nw couch #300 w: eroi.com
>> t: 503.290.3105 f: 503.228.4249
>>
>>
>> fb: fb.com/eROI <http://www.facebook.com/eROI>
>>
>>
>>
>>
>>
>>
>>
>>
>> On Nov 22, 2016, at 8:50 AM, Mayuresh <ma...@gmail.com>
>> wrote:
>>
>> Hi,
>>
>> How can I check for the last occurrence of a string in the response html
>> and only replace the last occurrence of it?
>>
>> I want to search for the last "meta" tag in the response and replace it
>> with something.
>>
>> How can I do this?
>>
>> Thanks,
>> Mayuresh
>>
>>
>>
>
>
> --
> -Mayuresh
>



-- 
-Mayuresh

Re: [users@httpd] Mod_Substitute - Match the last occurrence of a string in the response

Posted by Mayuresh <ma...@gmail.com>.
Hi Jason,

Each meta tag is appearing on a separate line. Will it work even then?
Trying it out any ways.

Thanks,
Mayuresh

On Tue, Nov 22, 2016 at 10:41 AM, Jason Brooks <ja...@eroi.com>
wrote:

> Hello,
>
> According to the Apache Glossary page
> <https://httpd.apache.org/docs/current/glossary.html#regex>, it’s all PCRE
> <http://www.pcre.org/> based.  So you should be able to use the
> “non-greedy” match.  So instead of .* which will match all instances, use
> .*?.  If you anchor it at the end of the string, something like (text you
> are matching against).*?$, then you can be certain of getting the last one.
>
> —jason
>
> Jason Brooks Systems Administrator
> eROI Performance is Art.
>
> m: 505 nw couch #300 w: eroi.com
> t: 503.290.3105 f: 503.228.4249
>
>
> fb: fb.com/eROI <http://www.facebook.com/eROI>
>
>
>
>
>
>
>
>
> On Nov 22, 2016, at 8:50 AM, Mayuresh <ma...@gmail.com>
> wrote:
>
> Hi,
>
> How can I check for the last occurrence of a string in the response html
> and only replace the last occurrence of it?
>
> I want to search for the last "meta" tag in the response and replace it
> with something.
>
> How can I do this?
>
> Thanks,
> Mayuresh
>
>
>


-- 
-Mayuresh

Re: [users@httpd] Mod_Substitute - Match the last occurrence of a string in the response

Posted by Jason Brooks <ja...@eroi.com>.
Hello,

According to the Apache Glossary page <https://httpd.apache.org/docs/current/glossary.html#regex>, it’s all PCRE <http://www.pcre.org/> based.  So you should be able to use the “non-greedy” match.  So instead of .* which will match all instances, use .*?.  If you anchor it at the end of the string, something like (text you are matching against).*?$, then you can be certain of getting the last one.

—jason

Jason Brooks	Systems Administrator
eROI	Performance is Art.
 
m:	505 nw couch #300	w:	eroi.com <http://eroi.com/>
t:	503.290.3105	f:	503.228.4249


fb:	fb.com/eROI <http://www.facebook.com/eROI>








> On Nov 22, 2016, at 8:50 AM, Mayuresh <ma...@gmail.com> wrote:
> 
> Hi,
> 
> How can I check for the last occurrence of a string in the response html and only replace the last occurrence of it?
> 
> I want to search for the last "meta" tag in the response and replace it with something.
> 
> How can I do this?
> 
> Thanks,
> Mayuresh


Re: [users@httpd] Mod_Substitute - Match the last occurrence of a string in the response

Posted by bob gailer <bg...@gmail.com>.
On 11/22/2016 11:50 AM, Mayuresh wrote:
> Hi,
>
> How can I check for the last occurrence of a string in the response 
> html and only replace the last occurrence of it?
>
> I want to search for the last "meta" tag in the response and replace 
> it with something.
Hi. I am new to Apache, so my questions and answers may not be adequate.

By "response" I assume Apache has received a request and you are dealing 
with the response to that request. There may be a "hook" in Apache that 
lets you intercept that response and edit it. Someone else will have to 
explain how  to handle that.

OR - What is generating the response? (Example: an HTML document, a PHP 
page you have written, a Python program, or what)?

Bob

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org