Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Why doesn't this regex work?

by mtmcc (Hermit)
on Aug 15, 2013 at 10:40 UTC ( [id://1049547]=note: print w/replies, xml ) Need Help??


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

I've looked through it with Regexp::Debugger.

Maybe (probably) I'm misunderstanding the question, but in your sample data, the hundred groups in each line seem to be the same. i.e. $1 + 1 will always not equal $2 in this data, for each line.

I think the second point is about the spaces. When the regex matches, it skips a space, so misses out on matching the next number to the first part of the regex.

For example, this inserts a newline before the 200s start:

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

But when you change the data to:

105 106 107 108 109 210 211 212 213 1115 1116 1117 1118 1119 1120 1121 1122 1123 12345 12346 12347 12348 12349 12350 12351 12353

the first part of the regex doesn't match the 109, because the match fails on the first space.

Apologies if I'm way off!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-20 01:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found