Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Text ersetzen - aber nicht innerhalb von Link-Tags

Received: by 10.68.230.68 with SMTP id sw4mr680615pbc.7.1333754155405;
        Fri, 06 Apr 2012 16:15:55 -0700 (PDT)
Path: r9ni26847pbh.0!nntp.google.com!news1.google.com!goblin3!goblin1!goblin.stu.neva.ru!noris.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Message-ID: <1764299.ZWGnKmheAe@PointedEars.de>
From: Thomas 'PointedEars' Lahn <PointedE...@web.de>
Reply-To: Thomas 'PointedEars' Lahn <use...@PointedEars.de>
Organization: PointedEars Software (PES)
Date: Sat, 07 Apr 2012 01:15:26 +0200
User-Agent: KNode/4.4.11
Subject: Re: Text ersetzen - aber nicht innerhalb von Link-Tags
Newsgroups: de.comp.lang.perl.misc
References: <jjjgif$rfd$1@nntp.de> <m34ntuy0b3.fsf@passepartout.tim-landscheidt.de> <jjlm3t$gi5$1@yggdrasil.mitch.h.shuttle.de> <3663297.nDa2CHAUnY@PointedEars.de>
Followup-To: de.comp.lang.perl.misc
MIME-Version: 1.0
Lines: 111
NNTP-Posting-Date: 07 Apr 2012 01:15:29 CEST
NNTP-Posting-Host: 91c8f968.newsspool3.arcor-online.net
X-Trace: DXC=badG`e^=<RM[6=1B@oB@@@McF=Q^Z^V3H4Fo<]lROoRA8kF<OcfhCOKo;k`8g]<bOGDZm8W4\YJNLJ`:FmdeL>K@fQ>EOE\[m`Kdi0X3^=ZD8N
X-Complaints-To: usenet-abuse@arcor.de
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 8Bit

Thomas 'PointedEars' Lahn wrote:

> Christian Garbs wrote:
>> Tim Landscheidt <t...@tim-landscheidt.de> wrote:
>>> | $_ = $a;
>>> | while (/(<a .*?<\/a>|FOO)/g)
>>> |   {
>>> |     substr ($_, pos () - length ($1), length ($1)) = 'BAR' if ($1 eq
>>> |     'FOO');
>>> |   }
>>  
>>> Irgendwie schaffe ich es aber nicht, die Zuweisung an $_
>>> durch ein "$a =~" zu ersetzen. Wenn length ('FOO') != length
>>> ('BAR') mĂŒsste man wohl noch pos () zuweisen.
>> 
>> Schöner Ansatz!  Das substr() kannst Du dann mit s///e umgehen:
>> 
>>  $a =~ s/(<a .*?<\/a>|FOO)/$1 eq 'FOO' ? 'BAR' : $1/ge;
>> 
>> Klappt auch mit unterschiedlichen LĂ€ngen.  Die Zeile muss ich mir
>> jetzt merken, die kann ich bestimmt auch noch öfter gebrauchen :-)
> 
> Zweifelhaft, siehe andere Antwort.  Abgesehen von ungĂŒltigem Markup:
> Dieser Ansatz funktioniert gemÀss aktuellem Working Draft schon mit HTML5
> nicht mehr, mit dem
> 
>   <a href="" title="</a>">b</a>
> 
> syntaktisch korrekt ist [1] und somit folgenden Parse-Unterbaum erzeugt:
> 
>   element(a)
>     attribute(href)
>       text("")
>     attribute(title)
>       text("</a>")
>     text("b")
> 
> (Interessanterweise hat der W3C-Validator daran auch als HTML 4.01 nichts
> auszusetzen.)
> 
> Mit obigem Ansatz wĂŒrde jedoch
> 
>    <a .*?</a>
> 
> auf
> 
>   <a href="" title="</a>
> 
> matchen.
> 
> ______
> [1] <http://www.w3.org/TR/html5/tokenization.html#attribute-value-double-
> quoted-state>

Eine PCRE, die *relativ* zuverlÀssig auf ein *nicht-verschachtelbares* 
Element mit *nicht-optionalem* Start- und End-Tag in einem *gĂŒltigen* HTML5-
Dokument matcht, wÀre demnach

  /<([a-z]+)         # element type name
   (?:               # attribute (optional)
     \s+[a-z]+       # attribute name
     (?:             # attribute value (optional)
       \s*
       (?:          
         =\s*
         (?:         
            "[^"]*"
           |'[^']*'
           |\S+
         )
       )?
     )?
   )*
   \s*\/?>
     .*?
   <\/\1>
  /isx

Im Fall von a-Elementen also:

  /<(a)              # element type name
   (?:               # attribute (optional)
     \s+[a-z]+       # attribute name
     (?:             # attribute value (optional)
       \s*
       (?:
         =\s*
         (?:
            "[^"]*"
           |'[^']*'
           |\S+
         )
       )?
     )?
   )*
   \s*\/?>
     .*?
   <\/\1>
  /isx

[Übersehen wurde bei dem zuvor vorgeschlagenen Ansatz ausserdem, dass 
ZeilenumbrĂŒche im Start-Tag sowie im Elementinhalt erlaubt (und bei lĂ€ngeren 
Tags oder Inhalten aufgrund besserer Lesbarkeit sogar empfehlenswert) sind.  
Das s-Flag (PCRE_DOTALL) löst dieses Problem.]

HTH

-- 
PointedEars

Please do not Cc: me. / Bitte keine Kopien per E-Mail.