Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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";

PJ
use strict; use warnings; use diagnostics;


Comment on Re: Breaking String
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2014-11-23 21:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (134 votes), past polls