Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Breaking String

by periapt (Hermit)
on Oct 17, 2008 at 19:27 UTC ( #717847=note: print w/replies, xml ) Need Help??

in reply to Breaking String

Text::Wrap is usually the best way to go in these matters. Here is a simple regex that has worked for me in the past.

use strict; use warnings; use diagnostics; my $inp = 'This sentence will have more than 120 characters and i want + to '. 'truncate this string into two lines containing 60 character +s each '. 'and ignore characters above 140 in length'; my $linelen = 60; my $matchlen = $linelen - 1; # this is what you get with your basic substring parsing; my $line01 = substr($inp,0,60); my $line02 = substr($inp,60,60); my $line03 = substr($inp,120); print '[',length($line01),'] : %',$line01,'%',"\n"; print '[',length($line02),'] : %',$line02,'%',"\n"; print '[',length($line03),'] : %',$line03,'%',"\n"; print "\n"; # this regex splits on a max of 59 chars plus one "terminating" char; # in this case this is just another word char (as defined by \w). this + is really # only relevant if the line of non-breaking characters is exactly line +len in size. # the very last capture is the rest of the text so, theoretically, you + could # repeat the pattern for as many match sequences as desire. useful if +the "terminator" # changes for each field. as a side note, the regex will discard all n +on-word chars # at the split location. if needed, you could change the \W+ to any cl +ass of chars # such as [\s,-:]+ $inp =~ m/^(.{0,$matchlen}\w??)\W+(.{0,$matchlen}\w??)\W+(.*)/; $line01 = ($1 || ''); $line02 = ($2 || ''); $line03 = ($3 || ''); print '[',length($line01),'] : %',$line01,'%',"\n"; print '[',length($line02),'] : %',$line02,'%',"\n"; print '[',length($line03),'] : %',$line03,'%',"\n"; print "\n"; # the added advantage of this construct is that it allows you to do so +mething # with the last matching character if needed $inp =~ m/^(.{0,$matchlen})(\w)??\W+(.{0,$matchlen})(\w)??\W+(.*)/; $line01 = ($1 || '').($2 || ''); $line02 = ($3 || '').($4 || ''); $line03 = ($5 || ''); print '[',length($line01),'] : %',$line01,'%',"\n"; print '[',length($line02),'] : %',$line02,'%',"\n"; print '[',length($line03),'] : %',$line03,'%',"\n"; print "\n";

use strict; use warnings; use diagnostics;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://717847]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2018-03-18 04:38 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (228 votes). Check out past polls.