Actually, I'd probably go with my own variant (see above), which happens to be faster than either of these.

Yours isn't the fastest for me, although I've added use warnings, use strict; and forced a scalar context unto the substitution:

#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); my $test_text = q|Wow, that was quick!<br/> Two points:<br/> 1) I only want space, not tabs or new lines - so shouldn't the \s be + replaced with " "? <br/> 2) Is there a difference between inkgmi's and GrandFather's entry? <b +r/> PS I thought executed regexs are experimental (so says the man page) - + is there a problem with them?<br/> |;
cmpthese(-3, { GrandFather => sub { local $_ = $test_text; scalar s/ ( +)/" " . ("&nbsp;" x length ($1))/ge }, ikegami => sub { local $_ = $test_text; scalar s/(?<= )( +)/'&nbsp;' x length($1)/eg }, Smylers => sub { local $_ = $test_text; scalar s/(?<= )( )/&nbsp;/g }, }); __END__ Rate ikegami Smylers GrandFather ikegami 22180/s -- -25% -31% Smylers 29772/s 34% -- -8% GrandFather 32268/s 45% 8% --

In reply to Re^6: Converting multiple spaces to nbsp by ikegami
in thread Converting multiple spaces to nbsp by eastcoastcoder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.