in reply to highlight and line breaks

Add your markup first. Then use something like the following to add your "linebreaks" (which I gather are newlines from reading your problem description):

s!((?:<span>|</span>|.){80})!$1\n!g;
That will allow you to treat your markup tags as a single character when trying to decide where to put your newlines. You may need the /s modifier to deal with existing newlines.

Edit: Fixed thinko. I meant /s rather than /m of course.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: highlight and line breaks
by glwtta (Hermit) on Jan 16, 2003 at 01:21 UTC
    this sounds more like the one-liner I was looking for, but the only problem is that a single character is not good enough, the tags have to be zero characters long for the wrapping purposes.

    thanks for all the great replies though, I am well impressed

      the tags have to be zero characters long for the wrapping purposes.

      That's more difficult. I would probably do it like this:

      $_ = "foo<span>bar</span>bazqux<span>quux</span>"; my $c=0; s( (<span>|</span>|.) ) { my $s = $1; $s =~ m!</?span>! ? $s : $s . (++$c % 4 ? '' : "\n"); }sxge; print; print "\n";

      That will show you how it works but in your case you'll need to change that '4' to '80' in the ++$c % 4 part.

      Update: added /s to regex so . would match "\n" as well.

      -sauoq
      "My two cents aren't worth a dime.";
      
        Not bad, but why try to match the tag again when you already let the engine match it before?
        $_ = "foo<span>bar</span>bazqux<span>quux</span>"; my $c=0; s( (<span>|</span>|(.)) ) { defined $2 ? $1 . (++$c % 4 ? '' : "\n") : $1; }sxge; print; print "\n";

        Makeshifts last the longest.

        Assuming you never have an empty <span></span> set, I believe the following should work, should it not?

        s{((?:(?:<span>)?.(?:</span>)?){80})}{$1\n}g

        bbfu
        Black flowers blossum
        Fearless on my breath