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


Hi Monks,

I want to match a regualar expression pattern.

The following array contains the following elements

@xmlfiles =( rtl_genoa_cet_300color-46-13-03.BIN.xml,rtl_genoa_cet_300color-46-13-03.BIN-summary.xml,rtl_genoa_cet_300color-47-13-04.BIN.xml, rtl_genoa_cet_300color-47-13-04.BIN-summary.xml,rtl_genoa_cet_300color.xml,rtl_genoa_cet_600color-1-08-01.BIN.xml,rtl_genoa_cet_600color-1-08-01.BIN-summ ary.xml,rtl_genoa_cet_600color-49-13-06.BIN.xml,rtl_genoa_cet_600color-49-13-06.BIN-summary.xml,rtl_genoa_cet_600color.xml );

I want a regular expression such that it matches only the files like rtl_genoa_cet_300color-46-13-03.BIN.xml,rtl_genoa_cet_300color-47-13-04.BIN.xml etc.

The expression should not match files like rtl_genoa_cet_300color-46-13-03.BIN-summary.xml,rtl_genoa_cet_300color-47-13-04.BIN-summary.xml,

rtl_genoa_cet_300color.xml,rtl_genoa_cet_600color.xml

Can any one help me on this?

Thanks in advance....

Regards

Anand

Replies are listed 'Best First'.
Re: Regular Expression required.
by grizzley (Chaplain) on Sep 18, 2008 at 11:32 UTC

    Can you specify criteria, e.g.:

    • match only names without 'summary' word and/or
    • match only names with 'dd-dd-dd' (d=digit) part and/or
    • match only names with '300color' string
    • ...

      I want to match the strings without 'summary' word and the strings like rtl_genoa_cet_300color-46-13-03.BIN.xml,rtl_genoa_cet_300color-47-13-04.BIN.xml

      The strings like rtl_genoa_cet_300color.xml should not be matched

        Once again: could you specify criteria?

        Maybe say which ones below should be matched? I tried to change one thing in every string:
        • stl_genoa_cet_300color-46-13-03.BIN.xml (1st block changed)
        • rtl_genoi_cet_300color-46-13-03.BIN.xml (2nd block changed)
        • rtl_genoa_cit_300color-46-13-03.BIN.xml (3rd block changed)
        • rtl_genoacet_300color-46-13-03.BIN.xml (no 3rd block)
        • rtl_genoa_cet_400color-46-13-03.BIN.xml (4th block, number changed)
        • rtl_genoa_cet_300bw-46-13-03.BIN.xml (4th block, type changed)
        • rtl_genoa_cet_300color.BIN.xml (no dd-dd-dd block)
        • rtl_genoa_cet_300color-99-99-99.BIN.xml (dd-dd-dd block changed)
        • rtl_genoa_cet_300color-46-13-03.USR.xml (name after '.' changed)
        • rtl_genoa_cet_300color-46-13-03BIN.xml (only one '.' in string)
        • rtl_genoa_cet_300color-46-13-03.BINxml (no '.'s in string)
        • rtl_genoa_cet_300color-46-13-03.BIN.png (last block (extension) changed)
        There are many more possibilities, but I hope you get the point. I (we) have no idea where the matched string must be exactly the same. Tell us.

        I think that /^rtl_genoa_cet_300color-4\d-13-0\d\.BIN\.xml$/ will do what you asked.

        Probably it doesn't do what you meant...

        Updated. This is shorter...

        Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Re: Regular Expression required.
by AnomalousMonk (Archbishop) on Sep 18, 2008 at 15:07 UTC
    My guess about the regex that might be needed for this (see the very pertinent comments of others about the need to specify a pattern precisely) would be something like:
    >perl -wMstrict -le "my $head = qr{ rtl_genoa_cet_300color- }xms; my $tail = qr{ \.BIN\.xml }xms; my $hyphenated_numbers = qr{ \d\d - \d\d - \d\d }xms; my $filename = qr{ \A $head $hyphenated_numbers $tail \z }xms; print qq{\noutput:}; for my $string (@ARGV) { print $string if $string =~ $filename; } " rtl_genoa_cet_300color-46-13-03.BIN.xml rtl_genoa_cet_300color-46-13-03.BIN-summary.xml rtl_genoa_cet_300color-47-13-04.BIN.xml rtl_genoa_cet_300color-47-13-04.BIN-summary.xml rtl_genoa_cet_300color.xml rtl_genoa_cet_600color-1-08-01.BIN.xml rtl_genoa_cet_600color-1-08-01.BIN-summary.xml rtl_genoa_cet_600color-49-13-06.BIN.xml rtl_genoa_cet_600color-49-13-06.BIN-summary.xml rtl_genoa_cet_600color.xml output: rtl_genoa_cet_300color-46-13-03.BIN.xml rtl_genoa_cet_300color-47-13-04.BIN.xml
Re: Regular Expression required.
by svenXY (Deacon) on Sep 18, 2008 at 11:34 UTC
    ...
    • or only *BIN*
    • ...

    Regards,
    svenXY