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

I am trying to build a regular expression for below files.

HIGH-LEVEL Status file for process (can be in below formats)

server_load_<ipaddress>.<ipaddress>.SKIP server_load_<ipaddress>.xyz_<ipaddress>.SKIP Example. server_load_IP100.69.82.21.IP100.69.82.81.SKIP server_load_IP100.69.82.21.ny_IP100.69.82.81.SKIP

Detailed Status file for process (can be in below formats)

server_load_<ipaddress>.<ipaddress>.xyx_ABC-2.SKIP server_load_<ipaddress>.ny_<ipaddress>.ABC_load.SKIP Example server_load_IP100.69.82.21.IP100.69.82.81.ny_AMER-2.SKIP server_load_IP100.69.82.21.ny_IP100.69.82.81.AMER_load.SKIP

My regular expression SHOULD NOT match any name from HIGH-LEVEL Status file for process. only that will work for Detailed Status file for process.

Tried using Look-ahead and look-behind concepts, but not able to get my best.

#!/usr/bin/perl use strict; use warnings; while (<DATA>) { if (/^(?<!server_load_IP100.69.82.21\s).*.SKIP/) {print "Good Match "; +} else {"Bad Match ";} print; } __DATA__ server_load_IP100.69.82.21.IP100.69.82.81.SKIP server_load_IP100.69.82.21.IP100.69.82.81.ny_AMER-2.SKIP
Output =============== Good Match server_load_IP100.69.82.21.IP100.69.82.81.SKIP ==> Ideall +y this is HIGH-LEVEL Status file for process should be "Bad Match " Good Match server_load_IP100.69.82.21.IP100.69.82.81.ny_AMER-2.SKIP

Any thoughts or guidance please. Thanks.

Replies are listed 'Best First'.
Re: Deriving File name convention with Look-ahead and look-behind assertion
by Eily (Monsignor) on Oct 19, 2016 at 14:39 UTC

    Your code sample doesn't make much sense. With the \s after the IP address (not present in the input string) the expression inside the look-behind assertion will always fail. But, with a litteral "failed" the regex as a whole won't ever match. So this can't be how you got that output. See How do I post a question effectively?

    Anyway, what about /(\w*IP(\d+\.){4}){2}(.*\.)?SKIP/ ? You just have to check if $3 is defined or not (or simple remove the ?) to see if you are in a simple or full match

    Edit: added \w* in front of IP to match the examples in the description of the problem (not just the examples in the __DATA__ section).

Re: Deriving File name convention with Look-ahead and look-behind assertion
by Laurent_R (Canon) on Oct 19, 2016 at 17:10 UTC
    You could simply count the number of dots (see for example the tr/// function), or split on dots and count the number of items returned by split.