Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Question about conditional regex capture

by TASdvlper (Monk)
on May 18, 2004 at 19:56 UTC ( [id://354415]=perlquestion: print w/replies, xml ) Need Help??

TASdvlper has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

I have another regexp question (seems to be my favorite topic).

I'm trying to parse the output of a cli command, and I'm looking for string, in each output line, that consists of digits seperated by periods. i.e.

0.1.4.1 0.1.5.2 0.1.6.3 0.1.7.4
but, my problem is, I don't want to capture the output if the 3 field, from the left, is a 0,1,2, or 3. But, it can start with it. I have this snippet:
if ( /.*(\d+\.\d+\.[4-9]\.\d+).*/ ) { print "$1 <br> \n"; }
But this will not work if the 3rd field is a double digit.

So, to elaborate more, the following I want to ignore:

0.1.1.1 0.1.2.2 0.1.3.3
but, these I do want to capture:
0.1.10.1 0.1.20.2 0.1.30.3
Basically, if the 3rd field is > 3, I want to capture it.

Any thoughts on to do this ???? Thx.

20040519 Edit by Corion: Changed title from 'Another regular expression'

Replies are listed 'Best First'.
Re: Question about conditional regex capture
by Fletch (Bishop) on May 18, 2004 at 20:02 UTC

    So just use capturing parens in your regexp and compare the number to 3. You don't have to do everything in a regexp.

    if( /(\d+\.\d+\.(\d+)\.\d+)/ and $2 > 3 ) { print $1, "<br />\n"; }
Re: Question about conditional regex capture
by kvale (Monsignor) on May 18, 2004 at 20:03 UTC
    You are on th right track. Gropuing lets you have alternatives
    if ( /.*(\d+\.\d+\.(\d\d+|[4-9])\.\d+).*/ ) { print "$1 <br> \n"; }
    This matches either a multidigit number or a single digit in 4..9. Note it could still be fooled by 01, etc, so check your input for that problem.

    -Mark

Re: Question about conditional regex capture
by cLive ;-) (Prior) on May 18, 2004 at 20:16 UTC
    if ( /(\d+\.\d+\.(?:\d{2,}|[4-9])\.\d+)/ ) { print "$1 <br> \n"; }
    cLive ;-)
Re: Question about conditional regex capture
by saskaqueer (Friar) on May 18, 2004 at 20:16 UTC

    update: I missed the fact that the string of digits with periods needs to be extracted from a line of additional text. The solution I posted below would only work once you have already extracted the digit/period string.

    # why use a regex? splitting is enough while ( <DATA> ) { print $_ if ( (split( /\./ ))[2] > 3 ); } __DATA__ 0.1.1.1 0.1.2.2 0.1.3.3 0.1.10.1 0.1.20.2 0.1.30.3
Re: Question about conditional regex capture
by Abigail-II (Bishop) on May 19, 2004 at 08:57 UTC
    For starters, anchor your regex, for two reasons: efficiency, and you prevent a match to happen by the regex engine starting to match at the second group.
    /^(\d+.\d+\.(\d{2,}|[4-9])\.\d+)/

    Abigail

Re: Question about conditional regex capture
by hv (Prior) on May 19, 2004 at 13:03 UTC

    You can also express this slightly more naturally, at the cost of a slightly less efficient regexp:

    if (/^(\d+\.\d+\.(?![0-3]\.)\d+\.\d+)/) { print ... }
    that is: match two numbers then (as long as the next number isn't in the range 0 .. 3) match two more numbers.

    Hugo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2024-04-18 08:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found