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;

In reply to Re: Breaking String by periapt
in thread Breaking String by harishnuti

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.