[Israel.pm] A simpler regex required

Peter Gordon peter at pg-consultants.com
Wed Aug 15 01:41:32 PDT 2007


Wouldn't it be cute and much more intuitive if we could write this? 

s!<td.*?>(.*?)</td>!$1 =~ s/\s/&nbsp;/g!eg


Peter

On Wed, 2007-08-15 at 08:50 +0300, Yuval Yaari wrote:
> Peter Gordon wrote:
> > Hi. 
> >
> > Let's suppose that I have the following lines in an HTML file. 
> > I want to substitute the spaces in the date part with non-breaking spaces (&nbsp;)
> >
> > <td  style="text-align: left" bgcolor="#92c1bb">Aug 12 23:59:59 2007 GMT</td>
> > <td  style="text-align: left" bgcolor="#92c1bb">Aug 12 23:59:59 2007 GMT</td>
> >
> > I came up with this line - but somehow it isn't aesthetic.
> >
> > s!(<td.*?>)(.*?)(</td>)!my $t1 = $1 ;my $t2 = $2 ; my $t3 = $3 ; $t2 =~ s/\s/&nbsp;/g ; "$t1$t2$t3" ;!egs ;
> >
> > Is there a nicer/cleaner way to write it?
> >   
> Using look-ahead and look-behind, I guess.
> 
> Oh, yeh, only variable length look-behind won't work in Perl 5.8.8... :-(
> I do like this trick in blead, though:
> 
> s{<td.*?> \K (.+?) (?=</td>)}
>   {(my $text = $1) =~ s/\s/&nbsp;/g; $text}egx;
> 
> \K is described in perlre as "Keep the stuff left of the \K, don't 
> include it in $&".
> 
> HTH (guess not :-)),
> 
>   ~Y
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl




More information about the Perl mailing list