You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Zimmi <zi...@gmail.com> on 2015/07/10 13:26:07 UTC

[users@httpd] lookaround in mod_substitute pattern

Hi list!

I was testing mod_substitute and lookaheads. I tested with the simple 
setup below (a simple string in an index.html, and a .htaccess file), 
and the lookahead yields expected result, the negative lookahead and the 
lookbehind have no effect.
The negative lookbehind has a behaviour that I do not understand.

Are only lookahead (?=foo) supported in mod_substitute, and not the 
other (negative lookahead, lookbehind and negative lookbehind) ?
Other syntax or did I miss something ?

And as a bonus question, why is the last test ( Substitute 
"s/a(?<!m)/Q/" ) changing all letters to 'Q' instead of not changing the 
string like the 2nd and the 3rd test ?

Thanks for sharing your insights!
Zimmi


SETUP
index.html :
Llamas are my favorite animals.

.htaccess :
AddOutputFilterByType SUBSTITUTE text/html

# Original string inside an index.html :
# Llamas are my favorite animals.

# 1. Lookahead, OK
# Expected : word 'favorite' changed:
# Llamas are my f4vorite animals.
Substitute "s/a(?=v)/4/"

# 2. Lookbehind, not OK : nothing happens
# Expected : word 'animals' changed:
# Llamas are my favorite animAls.
Substitute "s/a(?<=m)/A/"

# 3. Negative lookahead, not OK : nothing happens:
# Expected : all remaining 'a' changed to '&' except last one in word 
'animals':
# Ll&m&s &re my f4vorite &nimals.
Substitute "s/a(?!l)/&/"

# 4. Negative lookbehind, not OK : changes all remaining occurrences of 
'a' to 'Q' ? Why ?
# Expected : all remaining 'a' changed to 'Q' except last 'a' in word 
'animals'
#            and first 'a' in 'Llamas' if not changed by rules before.
Substitute "s/a(?<!m)/Q/"


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


Re: [users@httpd] lookaround in mod_substitute pattern

Posted by Yann Ylavic <yl...@gmail.com>.
Hi Zimmi,

On Fri, Jul 10, 2015 at 1:26 PM, Zimmi <zi...@gmail.com> wrote:
>
> Are only lookahead (?=foo) supported in mod_substitute, and not the other
> (negative lookahead, lookbehind and negative lookbehind) ?
> Other syntax or did I miss something ?

A lookbehind assertion needs its subject to be placed *after* it, so
eg. "a" preceded by "m" is "(?<=m)a".

So in your examples:

>
> # 2. Lookbehind, not OK : nothing happens
> # Expected : word 'animals' changed:
> # Llamas are my favorite animAls.
> Substitute "s/a(?<=m)/A/"

=> Substitute "s/(?<=m)a/A/"

>
> # 3. Negative lookahead, not OK : nothing happens:
> # Expected : all remaining 'a' changed to '&' except last one in word
> 'animals':
> # Ll&m&s &re my f4vorite &nimals.
> Substitute "s/a(?!l)/&/"

=> Substitute "s/a(?!\l)/&/"
(The pipe has special meaning otherwise, but I'm not sure about your
goal here...).

>
> # 4. Negative lookbehind, not OK : changes all remaining occurrences of 'a'
> to 'Q' ? Why ?
> # Expected : all remaining 'a' changed to 'Q' except last 'a' in word
> 'animals'
> #            and first 'a' in 'Llamas' if not changed by rules before.
> Substitute "s/a(?<!m)/Q/"

=> Substitute "s/(?<!m)a/Q/"


Regards,
Yann.

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