Did you try your solution? It doesn't add a trailing newline (nor fix the more important problem). Here's my next stab:

s{( (?:[^\n]{1,15})(?=\n|$) # Line requiring no wrapping | (?:[^\n]{0,14},)(?!\n) # Line that can be wrapped at comma | (?:[^\n]{15})(?!\n) # Line to be wrapped, not at comma )\n? }{$1\n}gx;

And just two test cases:

#!/usr/bin/perl -w use strict; my @lines= ( join( '', "0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,", 'c'x50, ",16,17,18,19,20,21,22,23,24,25,26,27", ), join( '', "0,1,2,3,4,\n5,6,7,8,9,10,11,12,13,14,15,", 'c'x50, ",16,17,18,19,20,21,22,23,24,25,26,27", ), ); for my $line ( @lines ) { print "\n"; $_= $line; s/([^\n]{0,14},|[^\n]{15})(?!\n)/$1\n/g; print "tye 1\n($_)\n"; $_= $line; s/([^\n]{0,14},|[^\n]{15})(?=[^\n])/$1\n/g; print "ikegami\n($_)\n"; $_= $line; s/((?:[^\n]{0,15}$)|(?:[^\n]{0,14},)|(?:[^\n]{15})(?=[^\n]))/$1\n/ +g; print "braveghost\n($_)\n"; $_= $line; s/((?:[^\n]{1,15}$)|(?:[^\n]{0,14},)|(?:[^\n]{15})(?=[^\n]))/$1\n/ +g; print "braveghost +1\n($_)\n"; $_= $line; s{( (?:[^\n]{1,15})(?=\n|$) # Line requiring no wrapping | (?:[^\n]{0,14},)(?!\n) # Line that can be wrapped at comma | (?:[^\n]{15})(?!\n) # Line to be wrapped, not at comma )\n? }{$1\n}gx; print "tye 2\n($_)\n"; } __END__ tye 1 (0,1,2,3,4,5,6, 7,8,9,10,11,12, 13,14,15, ccccccccccccccc ccccccccccccccc ccccccccccccccc ccccc,16,17,18, 19,20,21,22,23, 24,25,26, # Oops, extra newline 27) # Oops, no trailing newline ikegami (0,1,2,3,4,5,6, 7,8,9,10,11,12, 13,14,15, ccccccccccccccc ccccccccccccccc ccccccccccccccc ccccc,16,17,18, 19,20,21,22,23, # (Exact same problems as above) 24,25,26, # Oops, extra newline 27) # Oops, no trailing newline braveghost (0,1,2,3,4,5,6, 7,8,9,10,11,12, 13,14,15, ccccccccccccccc ccccccccccccccc ccccccccccccccc ccccc,16,17,18, 19,20,21,22,23, 24,25,26,27 ) # Oops, /two/ trailing newlines braveghost +1 (0,1,2,3,4,5,6, 7,8,9,10,11,12, 13,14,15, ccccccccccccccc ccccccccccccccc ccccccccccccccc ccccc,16,17,18, 19,20,21,22,23, 24,25,26,27 ) # Changing 0 to 1 fixes that tye 2 (0,1,2,3,4,5,6, 7,8,9,10,11,12, 13,14,15, ccccccccccccccc ccccccccccccccc ccccccccccccccc ccccc,16,17,18, 19,20,21,22,23, 24,25,26,27 ) # Also works ... braveghost (0,1,2,3,4, # Oops, extra newline 5,6,7,8,9,10, 11,12,13,14,15, ccccccccccccccc ccccccccccccccc ccccccccccccccc ccccc,16,17,18, 19,20,21,22,23, 24,25,26,27 ) braveghost +1 (0,1,2,3,4, # s/0/1/ doesn't fix this 5,6,7,8,9,10, 11,12,13,14,15, ccccccccccccccc ccccccccccccccc ccccccccccccccc ccccc,16,17,18, 19,20,21,22,23, 24,25,26,27 ) tye 2 (0,1,2,3,4, 5,6,7,8,9,10, 11,12,13,14,15, ccccccccccccccc ccccccccccccccc ccccccccccccccc ccccc,16,17,18, 19,20,21,22,23, 24,25,26,27 ) # Yay!

(update) Note there are three ways to write part of that: (?=\n|$) or (?![^\n]) or (?m:$). The last can be written as just $ by putting the m option on the end of the s/// construct. I leave the choice up to you.

- tye        


In reply to Re^4: Using pos() inside regexp (fixed) by tye
in thread Using pos() inside regexp by braveghost

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.