in reply to Perl regex. Why does this happen?

Why does this happen?

Because \s match all whitespace chars, including \n; thus \s{2} matches " \n" in your second line.

Based on the data you've provided, s[(\s{2})(?=\d)][2]g should do what you want.


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". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice. Not understood.

Replies are listed 'Best First'.
Re^2: Perl regex. Why does this happen?
by OneTrueDabe (Acolyte) on Jun 07, 2016 at 01:29 UTC
    Based on the data you've provided, s[(\s{2})(?=\d)][2]g should do what you want.
    Or (more simply, IMHO) use \h [horizontal whitespace] instead of \s

      \h added with Perl version 5.10.


      Give a man a fish:  <%-{-{-{-<

        If you want to be backward compatible, you can substitute [^\S\n] for \h.

        - tye