I believe that:

s/(?<=[[:alpha:]])\s+(?=[[:alpha:]])/ /g;

Will have the same effect as your posted expression. Although it will replace a single space character between two letters with a single space character (essentially a no-op), because it is a simpler pattern will actually take less time to execute. Below I am posting a simple benchmark I used to test this theory. If I have missed something please enlighten me.

#!/usr/local/bin/perl -w use strict; use Benchmark; my(@tests) = <DATA>; timethese(400, {his => sub{ s/(?<=[[:alpha:]])(?:\s\s+|[^\S ]+)(?=[[:a +lpha:]])/ /g foreach(@tests); }, mine => sub{s/(?<=[[:alpha:]])\s+(?=[[:alpha:]])/ /g f +oreach(@tests); }}); __DATA__ A sting with weird A string without weird Another variety with more wierd Anotherthingwithnospaces something odd soemthing normal [me@mylinux]$ ./space.pl Benchmark: timing 400 iterations of his, mine... his: 0 wallclock secs ( 0.07 usr + 0.00 sys = 0.07 CPU) @ 57 +14.29/s (n=400) (warning: too few iterations for a reliable count) mine: 0 wallclock secs ( 0.03 usr + 0.00 sys = 0.03 CPU) @ 13 +333.33/s (n=400) (warning: too few iterations for a reliable count)

They say that time changes things, but you actually have to change them yourself.

—Andy Warhol


In reply to Re^2: How to remove \n between two words but retain one space by JediWizard
in thread How to remove \n between two words but retain one space by Anonymous Monk

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.