scottb has asked for the wisdom of the Perl Monks concerning the following question:
Now I need for data for each 'int' to be optional, that is the line doesn't have to exist at all and in which case the array returned by evaluating the regex will contain an undef value in it's place. In other words, an interface doesn't neccesarily have to have a line dictating the number of flaps, and doesn't neccesarily have to have an IP address. I'd therefore come up with the following regex:int A Number of Flaps: 6 IP Address: 2.2.2.2 int B Number of Flaps: 8 IP Address 5.5.5.5 int C Number of Flaps: 9 IP Address 9.9.9.9
However, this doesn't find either of the flaps or the IP address. Interestingly though, in order to 'fix' the regular expression so that it does match, I merely need to remove the caret indicating that the optional component needs to be at the beginning of the line, and it matches, like so:^int\s+(\w+)\s* (?:^\s*Number\sof\sFlaps:\s(\d+)\s*)? (?:^\s*IP\sAddress:?\s(\S*)\s*)?
That works. With that, I find all three interfaces and their flaps and IP address. Another way I can make the regex find everything in this example (although it doesn't work for my real data in which the components are optional), is to remove the (?: )? clustering and force it to match that way:^int\s+(\w+)\s* (?:\s*Number\sof\sFlaps:\s(\d+)\s*)? (?:\s*IP\sAddress:?\s(\S*)\s*)?
Not really relivent in my situation, but interesting while I am attempting to understand the problem.^int\s+(\w+)\s* ^\s*Number\sof\sFlaps:\s(\d+)\s* ^\s*IP\sAddress:?\s(\S*)\s*
So the question is, what is it about the caret in my original regex that breaks the matching and makes the optional components not greedy? What do I mean by "not greedy"? Well, consider the following regex with a zero-width positive look-ahead assertion at the end to "pull down" the option components and try to force them to match:
That works too. It's the same regular expression before the zero-width positive look-ahead assertion. It surprises and stupifies me that it doesn't work without it because I would expect the optional components to try to match if they can. I mean it's not as if I had used (?: )?? to make it less greedy, but it seems to be behaving that way.^int\s+(\w+)\s* (?:^\s*Number\sof\sFlaps:\s(\d+)\s*)? (?:^\s*IP\sAddress:?\s(\S*)\s*)? (?=^int|\Z)
Anyone who can clue me in would be greatly appreciated.
Happy Holidays!
- Scott
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex confusion
by MarkusLaker (Beadle) on Dec 24, 2004 at 21:54 UTC | |
by scottb (Scribe) on Dec 25, 2004 at 06:34 UTC |