On 12.01.2011 23:15, Doug McNutt wrote:

> 
> The $& may be referring to the last match as it occurs in a previous loop 
> operation instead of what you think.
> 
> But why do you need the loop at all?  The g flag will repeat each substitute 
> over the whole text if the target is $_ instead of  $paragraf. In the loop 
> you probably don't want the g's at all.
> 
> $/ = undef;
> $paragraf = <>;
> $paragraf =~ s,</p>,$&<p class="links_normal">,g;
> $paragraf =~ s,\n\n<p$,</p><p,g;
> $paragraf =~ s,\n,<br>,g;
> $paragraf =~ s!\s{2,}!!g;
> print $paragraf;
> 
> I may well be missing something like being sure not to replace accidental 
> matches. Aren't those commas dangerous? You can use anything as the separator 
> but I prefer the pipe symbol when trying to avoid /\/ teepees.  You may also 
> need a ///s flag to allow matching of return characters. And come to think of 
> it are they \n or might they be \r in BBEdit tradition?
> 



Hello Doug!



Thank you for your reply. Whether I did not understand your suggestion,
or you did not understand my problem :-)

The problem was iterating over many "naked" (with out any html-tags)
text in a large text file and tag these found occurrences, and replace
the line breaks with <br>. Meanwhile I found the solution, but I don't
know, why this is working with a while-loop, and not with a
foreach-loop. Since I am looking into Perl - this is now about 10 years
- I have had always this comprehension problem between while and foreach
constructs. Also if this is not on topic for this list, could somebody
explain me this difference?

When using the debugger with the following script and the following
example, it is working. Used as a filter in BBEdit, the last paragraph
is not tagged. Strange! That means, my filter is probably not right ...

#!/usr/bin/perl

use strict;
use warnings;

$/ = undef;
$_ = <>;


while ($_ =~ m,(</p>\s+[^<]+?<p),g) {
        my $paragraf = $1;
        my $orig  = $1;
        $paragraf =~ s,(</p>)\n?,$1<p class="links_normal">,;
        $paragraf =~ s,\n\n<p$,</p><p,;
        $paragraf =~ s,\n,<br>\n,g;
        $paragraf =~ s!\s{2,}!!g;
        $paragraf =~ s!><!>\n<!g;
        s/$orig/$paragraf/;
}


print;


***test_file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd";><html>
<head>
<title>Death at a Funeral Script</title>
<meta name="description" content="Death at a Funeral script at the
Internet Movie Script Database.">
<meta name="keywords" content="Death at a Funeral script, Death at a
Funeral movie script, Death at a Funeral film script">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Language" content="EN">
<style type="text/css" media="all">
body {
        font-family: monospace;
}
h1
{
        margin: 2em;
        font-size: 1.4em;
        text-align: center;
}
p.mitte{
        font-size: 1em;
        text-align: center;
}
p.mitte_bold
{
        font-size: 1em;
        margin-left: 15em;
        font-weight: bold;
}
p.mitte_normal
{
        font-size: 1em;
        margin-left: 15em;
        font-weight: normal;
}
p.rechts
{
        font-size: 1em;
        text-align: right;
        font-weight: bold;
}
p.links_bold
{
        font-size: 1em;
        margin: 0 0 0 6em;
        font-weight: bold;
}
p.links_normal
{
        font-size: 1em;
        margin: 0em 0em 4em 6em;
        font-weight: normal;
}
p.new_page {
        margin-bottom: 6em;
}

</style>
</head>

<body>

<table width="100%">
<tr>
<td width="10%">
</td>
<td width="200">
<h1>DEATH AT A FUNERAL</h1>
<p class="mitte">
Written by
</p>
<p class="mitte">
 Chris Rock &amp; Aeysha Carr
</p>
<p class="mitte">
Based on "Death at a Funeral" by Dean Craig
</p>
<p class="rechts">
02/6/09
</p>
<p class="mitte_bold">
FADE IN:
</p>
<p class="mitte_normal">
MUSIC CUE: "DON'T WORRY, BE HAPPY" by Bobby McFerrin plays as we
</p>
<p class="mitte_bold">
BEGIN CREDITS
</p>
<p class="links_bold">INT. FUNERAL HOME - DAY
</p>
          We pan across a room filled with caskets.

<p class="links_bold">EXT. CHURCH - DAY
</p>
          People somberly walk into a church.

<p class="links_bold">EXT. FUNERAL HOME - DAY
</p>
          We see the signs outside of various funeral homes.

<p class="links_bold">EXT. CEMETERY - DAY
</p>
          We close in on a HEADSTONE that reads: "DEATH AT A FUNERAL".

<p class="links_bold">EXT. CHURCH - DAY
</p>
          ASIAN PALL BEARERS carry a coffin into a church.

<p class="links_bold">EXT. CEMETERY - DAY
</p>
          We push in on another headstone that reads: "Starring Chris
          Rock". As we push through the cemetery, we see various co-
          stars' names chiseled on headstones.
          We see a fleet of hearses driving through a cemetery.

<p class="links_bold">INT. FLORIST - DAY
</p>
          We see a FLORIST making a funeral arrangement.

<p class="links_bold">EXT. CEMETERY - DAY
</p>
          CLOSE UP of another headstone that has the director's name
          chiseled on it.

<p class="mitte_bold">END CREDITS
</p>
<p class="links_bold">INT. LIVING ROOM - DAY
</p>
          MUSIC CUE: The music fades out.
          TIGHT SHOT on AARON (CHRIS ROCK) thirties, dressed in a black
          suit and tie. He looks straight ahead with a somber expression.
          The camera pulls back to reveal he is watching FOUR UNDERTAKERS
          (INCLUDING BRIAN) place a coffin on a raised platform.

<p class="mitte_bold">(CONTINUED)
</p>
</td></tr></table>
</body>
</html>

-- 
You received this message because you are subscribed to the 
"BBEdit Talk" discussion group on Google Groups.
To post to this group, send email to bbedit@googlegroups.com
To unsubscribe from this group, send email to
bbedit+unsubscr...@googlegroups.com
For more options, visit this group at
<http://groups.google.com/group/bbedit?hl=en>
If you have a feature request or would like to report a problem, 
please email "supp...@barebones.com" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>

Reply via email to