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

S

Sandman

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)>].
 
G

Gunnar Hjalmarsson

Sandman said:
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;
 
G

gnari

Gunnar Hjalmarsson said:
Sandman said:
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
 
G

Gunnar Hjalmarsson

gnari said:
Gunnar said:
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.
 
G

gnari

Gunnar Hjalmarsson said:
gnari said:
Gunnar said:
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
 
S

Sandman

Gunnar Hjalmarsson said:
Sandman said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top