Hi,

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:

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
I am using the following regular expression for this example. The number of 'int's can change on me.
^(?:int)\s+(\w+)\s*?\n (?:\s*Number\sof\sFlaps:\s(\d+)\s*?\n)? (?:\s*IP\sAddress:?\s([\d\.]+)\s*?\n)? (?=^int|\Z)
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.

Is there any way I can have my cake and eat it too?

Thanks in advance,
Scott


In reply to Making regex /g match continuously by scottb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.