in reply to Re: Why doesn't this regex work?
in thread Why doesn't this regex work? (Solved!)

Have you tried use re 'debug';

Yes. But it didn't help. It shows that matches do occur; but doesn't explain why the newlines are never inserted.

It seems your example data miss a digit at the end of each number.

My mistake trying to simplify the data. Two corrections are possible:

  1. Remove one \d from each side of the regex:
    while( <DATA> ) { s[\s(\d+)\d\K\s(?=(\d+)\d\s)]{ print "$1:$2"; $1 + 1 == $2 ? "\n" : ' ' }ge; print; }
  2. Add a digit to the end of each number in the data:
    1051 1061 1071 1081 1091 1101 1111 1121 1131 11151 11161 11171 11181 11191 11201 11211 11221 11231 123451 123461 123471 123481 123491 123501 123511 123531
as you know what the regex should do

The idea of the regex is to match each pair of numbers in a line and capture the first d-2 digits of each number.

Eg. Match the pair and capture the first two digits of each:  (10)91 (11)01.

Then if $1+1 (10+1) == $2 (11) replace the space between them with a newline.

The \K prevents the number before the replaced space being replaced. And the lookahead prevents the number after ebing replaced.


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.