[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Perl regex

Hello list,

A little perl question, hope you don't mind:

I need to put line breaks in a string every 75 (or less) characters.  I
have everything working fine with one exception.  Whenever a $ occurs
within the string my substitution (below) ceases to work.

  $string =~ s/^$substring/$replacement/;

Here's the (possibly relevant) code:

First I strip out all carriage returns and excess spaces then I feed the
string through this loop, which works perfectly in all cases I have
encountered ... except of course those containing $:

$offset = 75;
while ($offset < length($string)) {
  # Find last space
  $position    = rindex($string, " ", $offset);
  $substring   = substr($string, 0, $position+1);
  $replacement = substr($string, 0, $position) . "\n";
  $string =~ s/^$substring/$replacement/;
  $offset = $offset + 75;

Thanks for any suggestions, including how to make my loop more efficient


Reply to: