tipthepizzaguy has asked for the wisdom of the Perl Monks concerning the following question:

If the string has 30 or more consecutive characters without a space, I want to insert a space before the 31st character. I've tried the following code but it doesn't seem to work:
$string =~ s/([^\s](30))/$1\s/g;
  • Comment on How do I insert a space after 30+ consecutive non-space characters?
  • Download Code

Replies are listed 'Best First'.
Re: How do I insert a space after 30+ consecutive non-space characters?
by jmcnamara (Monsignor) on May 23, 2002 at 07:18 UTC
Re: How do I insert a space after 30+ consecutive non-space characters?
by amarceluk (Beadle) on May 23, 2002 at 14:45 UTC
    jmcnamara is right, of course. If I may venture some further explanation: Remember that character classes and ranges, like a-z and \s, represent any of a number of characters, and are only used in the "find" rather than the "replace" part of a regex. It's impossible to use \s in a replacement because \s could be any one of a space, \t, \n, \r, or \f. As jmcnamara demonstrated, you need to specify the exact character you want to use as the replacement.