Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^4: Using pos() inside regexp (fixed)

by tye (Sage)
on Oct 08, 2010 at 17:48 UTC ( [id://864254]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Using pos() inside regexp (no /e)
in thread Using pos() inside regexp

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        

Replies are listed 'Best First'.
Re^5: Using pos() inside regexp (fixed)
by braveghost (Acolyte) on Oct 09, 2010 at 07:28 UTC
    You are my hero :)
    Yes, i've tried my solution, and extra new line at the end is not a big problem.
    But \n in the middle of the initial string is a possible situation, so thanks a large for you attention!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-26 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found