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

Have you tried
use re 'debug';
Its output is very long, but as you know what the regex should do, you might understand it.

Update: It seems your example data miss a digit at the end of each number. I tried it with a different data:

2120 2140 2180 2197 2200 2203 2205 2234 2238 2259 2280 2299 2401

If I understood your specification, the following should do the work, being a bit more readable:

s/\b(\d*)(\d\d) (?=\1\d\d\b)/$1$2_/g; s/ /\n/g; s/_/ /g;
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Why doesn't this regex work?
by BrowserUk (Patriarch) on Aug 15, 2013 at 10:11 UTC
    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.