Regexp: substituting \n with <br /> except in certain places

  • Follow


Textblock (indented for clarity):

    Hello, my nickname is Sandman, here is
    a list of things I like:

    <ol>
        <li> Perl
        <li> PHP
        <li> MySQL
    </ol>

I want to replace newlines with "<br />\n" in this textblock, for displaying it 
to a browser - but I don't want to replace it inside some specified html-tags - 
in this case "ol", but also "ul", "pre" and "xmp".

So, the resulting text block should be:

    Hello, my nickname is Sandman, here is<br />
    a list of things I like:<br />
    <br />
    <ol>
        <li> Perl
        <li> PHP
        <li> MySQL
    </ol>

Is there a fancy regexp for this? I know how to exclude certain characters with 
the [^...] 'switch', but what about whole blocks of text? In pseudo-code: 
[^<(ol|ul|pre|xmp)>].

-- 
Sandman[.net]
0
Reply mr249 (3193) 8/16/2004 10:16:45 AM

Sandman wrote:
> Textblock (indented for clarity):
> 
>     Hello, my nickname is Sandman, here is
>     a list of things I like:
> 
>     <ol>
>         <li> Perl
>         <li> PHP
>         <li> MySQL
>     </ol>
> 
> I want to replace newlines with "<br />\n" in this textblock, for
> displaying it to a browser - but I don't want to replace it inside
> some specified html-tags - in this case "ol", but also "ul", "pre"
> and "xmp".
> 
> So, the resulting text block should be:
> 
>     Hello, my nickname is Sandman, here is<br />
>     a list of things I like:<br />
>     <br />
>     <ol>
>         <li> Perl
>         <li> PHP
>         <li> MySQL
>     </ol>

How about:

     $text =~ s{(<(ol|ul|pre|xmp)[^>]*>.*?</\2>\n?)|\n}
               { $1 ? $1 : "<br />\n" }egis;

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
0
Reply Gunnar 8/16/2004 1:40:18 PM


"Gunnar Hjalmarsson" <noreply@gunnar.cc> wrote in message
news:2obs7iF8soh3U1@uni-berlin.de...
> Sandman wrote:
> > Textblock (indented for clarity):
> >
> >     Hello, my nickname is Sandman, here is
> >     a list of things I like:
> >
> >     <ol>
> >         <li> Perl
> >         <li> PHP
> >         <li> MySQL
> >     </ol>
> >
> > I want to replace newlines with "<br />\n" in this textblock, for
> > displaying it to a browser - but I don't want to replace it inside
> > some specified html-tags - in this case "ol", but also "ul", "pre"
> > and "xmp".
> >
> > So, the resulting text block should be:
> >
> >     Hello, my nickname is Sandman, here is<br />
> >     a list of things I like:<br />
> >     <br />
> >     <ol>
> >         <li> Perl
> >         <li> PHP
> >         <li> MySQL
> >     </ol>
>
> How about:
>
>      $text =~ s{(<(ol|ul|pre|xmp)[^>]*>.*?</\2>\n?)|\n}
>                { $1 ? $1 : "<br />\n" }egis;

not bad.
if an extra  '<br />' is acceptable after the end tag,
we can get rid of the /e:

      $text =~ s{(<(ol|ul|pre|xmp)[^>]*>.*?</\2>\n?)|\n}
                {<br />\n}gis;

gnari



0
Reply gnari 8/16/2004 7:07:01 PM

gnari wrote:
> Gunnar Hjalmarsson wrote:
>> 
>>How about:
>>
>>     $text =~ s{(<(ol|ul|pre|xmp)[^>]*>.*?</\2>\n?)|\n}
>>               { $1 ? $1 : "<br />\n" }egis;
> 
> not bad.

Thanks. :)

> if an extra  '<br />' is acceptable after the end tag,
> we can get rid of the /e:
> 
>       $text =~ s{(<(ol|ul|pre|xmp)[^>]*>.*?</\2>\n?)|\n}
>                 {<br />\n}gis;

Don't understand. That would not just add extra '<br />'s, but it 
would remove the identified HTML blocks.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
0
Reply Gunnar 8/16/2004 8:15:52 PM

"Gunnar Hjalmarsson" <noreply@gunnar.cc> wrote in message
news:2ocjeiF8rlksU1@uni-berlin.de...
> gnari wrote:
> > Gunnar Hjalmarsson wrote:
> >>
> >>How about:
> >>
> >>     $text =~ s{(<(ol|ul|pre|xmp)[^>]*>.*?</\2>\n?)|\n}
> >>               { $1 ? $1 : "<br />\n" }egis;
> >
> > not bad.
>
> Thanks. :)
>
> > if an extra  '<br />' is acceptable after the end tag,
> > we can get rid of the /e:
> >
> >       $text =~ s{(<(ol|ul|pre|xmp)[^>]*>.*?</\2>\n?)|\n}
> >                 {<br />\n}gis;
>
> Don't understand. That would not just add extra '<br />'s, but it
> would remove the identified HTML blocks.

indeed. of course I meant
       $text =~ s{(<(ol|ul|pre|xmp)[^>]*>.*?</\2>\n?)|\n}
                 {$1<br />\n}gis;

gnari




0
Reply gnari 8/16/2004 10:01:22 PM

In article <2obs7iF8soh3U1@uni-berlin.de>,
 Gunnar Hjalmarsson <noreply@gunnar.cc> wrote:

> Sandman wrote:
> > Textblock (indented for clarity):
> > 
> >     Hello, my nickname is Sandman, here is
> >     a list of things I like:
> > 
> >     <ol>
> >         <li> Perl
> >         <li> PHP
> >         <li> MySQL
> >     </ol>
> > 
> > I want to replace newlines with "<br />\n" in this textblock, for
> > displaying it to a browser - but I don't want to replace it inside
> > some specified html-tags - in this case "ol", but also "ul", "pre"
> > and "xmp".
> > 
> > So, the resulting text block should be:
> > 
> >     Hello, my nickname is Sandman, here is<br />
> >     a list of things I like:<br />
> >     <br />
> >     <ol>
> >         <li> Perl
> >         <li> PHP
> >         <li> MySQL
> >     </ol>
> 
> How about:
> 
>      $text =~ s{(<(ol|ul|pre|xmp)[^>]*>.*?</\2>\n?)|\n}
>                { $1 ? $1 : "<br />\n" }egis;

Thanks, works perfectly!

-- 
Sandman[.net]
0
Reply Sandman 8/17/2004 11:19:04 AM

5 Replies
30 Views

(page loaded in 0.101 seconds)

Similiar Articles:




7/27/2012 11:51:36 PM


Reply: