in reply to Re: Re: highlight and line breaks
in thread highlight and line breaks

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.";

Replies are listed 'Best First'.
Re^4: highlight and line breaks
by Aristotle (Chancellor) on Jan 16, 2003 at 09:12 UTC
    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.

(counting chars but ignoring SPANs) Re4: highlight and line breaks
by bbfu (Curate) on Jan 16, 2003 at 07:30 UTC

    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

      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

      No.

      For illustrative purposes, assume that you want to match 10 characters rather than 80. That won't change the behavior. Let's try it on something simple like "<span>1</span>":

      #!perl $_ = "<span>1</span>"; s{((?:(?:<span>)?.(?:</span>)?){10})}{$1\n}g; print "$_\n"; __END__ <span>1</s pan>
      What happened? Well, perl tries hard to make the expression match. When it doesn't succeed at first by matching the initial span tag with the first optional group, it backtracks and matches that first "<" with the dot. This isn't restricted to one or two edge cases either. It may work for "<span>foo</span>" but only because "foo</span>" is exactly 10 characters. It won't work for "<span>fo</span>" or "<span>foob</span>". Of course, the examples would be different if you are trying to count 80 characters but the bug is the same.

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

        Of course you're right. It felt like I was missing something last night when I was testing it but all of my tests ended with enough non-span characters that it seemed to work. :/

        bbfu
        Black flowers blossum
        Fearless on my breath