I have a set of large files of numbers output by some program, that are in a very wide layout -- ie. there are many numbers listed per line separated by spaces -- which is convenient for the program but not for human inspection.

I wanted to 'wrap' the lines at a convenient place. Whilst the numbers are not sequential, they are ordered and so I chose to wrap them such that all the numbers within each 100 range (eg. nnn100 .. nnn199 ) are on a single line.

To clarify, the input (vastly cut down) looks like this:

105 106 107 108 109 110 111 112 113 1115 1116 1117 1118 1119 1120 1121 1122 1123 12345 12346 12347 12348 12349 12350 12351 12353

And I decided to wrap it to look like this:

105 106 107 108 109 110 111 112 113 1115 1116 1117 1118 1119 1120 1121 1122 1123 12345 12346 12347 12348 12349 12350 12351 12353

I though to do this by replacing the space between (n1)\d\d and (n1+1)\d\d with a newline. Hence I tried the following regex:

#! perl -slw use strict; while( <DATA> ) { s[\s(\d+)\d\d\K\s(?=(\d+)\d\d\s)]{ $1 + 1 == $2 ? "\n" : ' ' }ge; print; } __DATA__ 105 106 107 108 109 110 111 112 113 1115 1116 1117 1118 1119 1120 1121 1122 1123 12345 12346 12347 12348 12349 12350 12351 12353

But it does nothing. I've since done the job (with multiple passes due to the fixed-length look-behind limitation) of:

perl -ple"s[(?<=\s(\d{3})\d\d)\s(?=(\d{3})\d\d\s)][$1+1==$2 ? qq[\n] : + ' ' ]ge" in > out

But I still don't understand why the above regex fails?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Why doesn't this regex work? (Solved!) by BrowserUk

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.