scottb has asked for the wisdom of the Perl Monks concerning the following question:
I have some large batches of data that I am matching with a regex and the /g option to repeat. Here is a simplified example of the data:
I am using the following regular expression for this example. The number of 'int's can change on me.int A Number of Flaps: 6 IP Address: 2.2.2.2 int B Number of Flaps: 8 IP Address 5.5.5.5 unpredictable data int C Number of Flaps: 9 IP Address 9.9.9.9
The problem is thus: the regex matches 'A' and 'C', not 'B', because of the unpredictable data that has sown up in 'B'. This is of course correct, but my desired behaviour is for the regex to fail because of the fact that 'B' didn't match. In other words, I want the next iteration of the regex to have to pick up where the last one left off, so that /g is not allowed to skip over sections of text it doesn't match. Is this possible? I know but I could write a full regex and not use /g, but the number of 'int's does change. I desire the best of both worlds.^(?:int)\s+(\w+)\s*?\n (?:\s*Number\sof\sFlaps:\s(\d+)\s*?\n)? (?:\s*IP\sAddress:?\s([\d\.]+)\s*?\n)? (?=^int|\Z)
Is there any way I can have my cake and eat it too?
Thanks in advance,
Scott
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Making regex /g match continuously
by Eimi Metamorphoumai (Deacon) on Jan 07, 2005 at 18:56 UTC | |
by scottb (Scribe) on Jan 07, 2005 at 20:06 UTC | |
by Roy Johnson (Monsignor) on Jan 07, 2005 at 20:18 UTC | |
by scottb (Scribe) on Jan 07, 2005 at 20:23 UTC | |
by Roy Johnson (Monsignor) on Jan 07, 2005 at 20:39 UTC | |
| |
by Eimi Metamorphoumai (Deacon) on Jan 07, 2005 at 20:37 UTC | |
|
Re: Making regex /g match continuously
by Roy Johnson (Monsignor) on Jan 07, 2005 at 19:18 UTC | |
by Eimi Metamorphoumai (Deacon) on Jan 07, 2005 at 19:39 UTC |