Re: [pcre-dev] cannot find regex for selectively replacing h…

Page principale
Supprimer ce message
Auteur: Roman Blöth
Date:  
À: Sheri
CC: pcre-dev
Sujet: Re: [pcre-dev] cannot find regex for selectively replacing html
Dear Sheri,


Sheri schrieb:
>> The catch is not to find the <img ...>-occurrences and surround them
>> with a hyperlink, the problem is NOT TO do it, when the <img>-tag
>> ALREADY IS surrounded by a hyperlink.
>>
>> The pattern I use is the following:
>>
>>     /(<img.*?[^>]*>)/i

[..]

> You need to look at lookahead and lookbehind assertions. They will let
> you spell out what must or must not come before and after matches. If
> its really as straightforward as not ">" before and not "<" after, it
> might look like this:
>
> /(?<!>)(<img.*?[^>]*>)(?!<)/i
>
> That should give you the same matches you had before, except only when not immediately preceded with > or followed by <. It should also work equally well without surrounding the main match with parentheses, you could still reference the whole match by using $0 instead of $1.
>
> Lookbehind assertions must be fixed length. In situations where you want to use a quantifier, instead of a lookbehind assertion you can use \K to reset the start of the match. \K is a relatively recent feature, your php would have to be using PCRE 7.2 or above.

Well, this is exactly what I need! Thank you a thousand times. I was
hoping I would never need to cope with lookbehind assertions, but now
I'll have to climb the learning courve...


Best regards,
Roman.