Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Word wrapping using a regex.

by Anonymous Monk
on Dec 06, 2011 at 16:50 UTC ( #942058=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I'm trying to have a word wrapping using a regex in Perl. What I would like is about every 50 characters or so to check for the next white-space occurrence and replace that space with a new line, and then do this for the whole string. I'd like to avoid looping one character at a time or using substr or placing the value into an array if possible. The code sample I have works, I'd like one's option and suggestion if there is a better way or a better regular expression to accomplish this.
Here is the code:
#!/usr/bin/perl -w use strict; my $string = 'I'm trying to have a word wrapping using a regex in Perl +. What I would like is about every 50 characters or so to check for the next white-space occurrence and replace that sp +ace with a newline, and then do this for the whole string. I'd like to avoid looping one character at a time or using substr or p +lacing the value into an array if possible. The code sample I have works, I'd like someone option and suggestion i +s there a better way or better regular expression to accomplish this. +'; $string =~ s/(.{1,50}\S|\S+)\s+/$1\n/mg; print "$string\n"; exit;
Thanks for looking!

Comment on Word wrapping using a regex.
Download Code
Replies are listed 'Best First'.
Re: Word wrapping using a regex.
by MidLifeXis (Monsignor) on Dec 06, 2011 at 16:53 UTC

    Would Text::Wrap be a reasonable solution, or must you use a regexp?

    --MidLifeXis

      Yeah, I have to use regexp!

        Smells like homework.

Re: Word wrapping using a regex.
by TJPride (Pilgrim) on Dec 06, 2011 at 22:07 UTC
    Your regex is obviously not working properly, because it prints beyond the boundary of 50 characters. Here's another option:

    use strict; use warnings; my $string = "I'm trying to have a word wrapping using a regex in Perl +. What I would like is about every 50 characters or so to check for +the next white-space occurrence and replace that space with a newline +, and then do this for the whole string. I'd like to avoid looping o +ne character at a time or using substr or placing the value into an a +rray if possible. The code sample I have works, I'd like someone opti +on and suggestion is there a better way or better regular expression +to accomplish this."; print '1234567890'x5 . "\n"; print wrap($string, 50) . "\n"; sub wrap { my ($s, $wrap) = @_; my (@lines, $line); while (length $s > $wrap) { ### Take one extra character so we can see if ### we're wrapping inside a word or not $line = substr($s, 0, $wrap+1); $line =~ s/\s+(\S*)$//; push @lines, $line; ### Add any word fragments back on $s = $1 . substr($s, $wrap); $s =~ s/^\s+//; } return join "\n", @lines; }
      I think to make this work we need to add another line at the end to tell it what to do with the last of the string that is less than $wrap.
      sub wrap { my ($s, $wrap) = @_; my (@lines, $line); while (length $s > $wrap) { ### Take one extra character so we can see if ### we're wrapping inside a word or not $line = substr($s, 0, $wrap+1); $line =~ s/\s+(\S*)$//; push @lines, $line; ### Add any word fragments back on $s = $1 . substr($s, $wrap); $s =~ s/^\s+//; } push @lines, $s; ## Adds the the last of the $s string to @lines return join "\n", @lines; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://942058]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2015-07-28 16:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (258 votes), past polls