Hi Anonymous,

The /s modifier means:

Treat the string as single line. That is, change "." to match any character whatsoever, even a newline, which normally it would not match.

So if I understand your question correctly, simply removing that modifier should do what you want:

my $len = 20; # 345678901234567890 my $text = <<'ENDTXT'; One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve Thirteen Fourteen ENDTXT my @lines = $text =~ /(.{1,$len}\W)/gm; # remove leftover whitespace at ends of lines s/\s+$// for @lines; print "<$_>\n" for @lines; __END__ <One> <Two Three Four Five> <Six Seven> <Eight Nine Ten> <Eleven Twelve> <Thirteen Fourteen>

Note that there's also the core module Text::Wrap that you could take a look at. Update 2: A an example of Text::Wrap that does the same thing as is similar to the above. Uncomment the tr/// operation to reflow the entire text:

use Text::Wrap; $Text::Wrap::columns = 20; #$text=~tr/\n/ /; print wrap('', '', $text);

Update 1: The following modification to the regex eliminates the need for the s/\s+$// for @lines; above (works because \s includes newline). Update 3: Actually, the following doesn't behave the same way the original regex does. It's hard to make an alternative suggestion without knowing what your intentions are here: do you definitely want to include one more non-word character at the end of the matched string, even whitespace, or did you perhaps mean a word boundary \b? If you could provide some sample input and expected output for different cases, and/or explain more about how you want the splitting to occur, that would help in making an alternate suggestion.

my @lines = $text =~ /(.{1,$len})\s+/gm;

Hope this helps,
-- Hauke D


In reply to Re: Split string using regex on \n or max line length (updated x3) by haukex
in thread Split string using regex on \n or max line length by Anonymous Monk

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.