[thelist] Perl - regular expressions

Dean Mah dmah at shaw.ca
Sat Dec 8 18:53:38 CST 2001


Mal Wins writes:

> Next time, I'll try to remember he wanted a regex.
> doh.
> Above was much closer to the mark
> 
> $var =~ /(.{1,99})[\s\n]/;
>    print "$1\n\n";
> print $';


This only works for the first couple of segments.  Using the loop
method it doesn't matter how long the text is, it will iterate through
the entire thing and break it into <= 99 character segments.


> Works far as I can tell, a new line before 99 catches though.  Back
> to lurking :)


You're right.  So instead try something like:

   while ($var =~ /(.{1,99})[\s\n]/gs) {
       print "$1\n\n";
   }

That will treat your string as a single line so the '.' will match
newlines as well.

Dean




More information about the thelist mailing list