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

I have the following line of code in a script:

 .Pattern = "(^c|l|j|s|o)\d{4}[_](\d*?.dat|\d*?.idx)"

This is being used to delete any files starting with C, L, J, S or O followed by the '_' and 0 or more digits then the extension. My issue is I need to modify this single line to delete all files as stated above AND files that do not have the '_' and 0 or more numbers in it.

Problem is I do not want to delete a files named C1234ABC.IDX. 1 letter, 4 digits, followed by either the extension, or _# or _## then extension. I have been playing around with:

 .Pattern = "(^c|l|j|s|o)\d{4}\w*?(\d*?.dat|\d*?.idx)"

but this did not do anything at all. Any ideas or suggestions to help resolve this would be great.

Thanks!!

Replies are listed 'Best First'.
Re: Regular Expression Assistance
by toolic (Bishop) on May 17, 2013 at 20:14 UTC
    Not sure I follow, but you can build on this:
    use warnings; use strict; while (<DATA>) { chomp; if (/ ^ [cljso] \d{4} _ \d*? \. (dat|idx) $ /ix) { print "$_ : match\n"; } else { print "$_ : no match\n"; } } __DATA__ C1234ABC.IDX C1234_ABC.IDX C1234_.IDX

    prints:

    C1234ABC.IDX : no match C1234_ABC.IDX : no match C1234_.IDX : match
Re: Regular Expression Assistance
by Laurent_R (Canon) on May 17, 2013 at 20:51 UTC

    So you want to match: C1234_.IDX, C1234_45.IDX and C1234.IDX but not C1234ABC.IDX. Is this correct?

    If so, the pattern could be something like this :

    /^([cljso]\d{4}(_\d*)?\.(dat|idx))$/

    i.e. start of string, one of the five letters cljso, 4 digits, an optional ("_" followed by any number of digits), followed by a dot and an extension chosen among dat and idx at the end of the string.

    If this is not what you need, please be clearer on your requirement.

Re: Regular Expression Assistance
by frozenwithjoy (Priest) on May 17, 2013 at 22:32 UTC
    You should plug your regex pattern into this regex visualization tool. It will help you see some problems with your pattern. For example, your ^ indicator for beginning of line is only applying to the c, not ljso.

      This tool is just great. I have just bookmarked it, I am sure it will be useful to me.

Re: Regular Expression Assistance (data?)
by LanX (Saint) on May 17, 2013 at 20:29 UTC
    Hmm ... could you please provide desired input and output data?

    That would facilitate finding a real solution w/o spending time on speculative stuff.

    BTW I don't understand why .Pattern starts with a dot, this is not Perl syntax.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: Regular Expression Assistance
by eye (Chaplain) on May 18, 2013 at 18:40 UTC
    Some good suggestions above.

    As long as we are commenting on a regex that is assigned to a variable, we should mention that the qr quoting operator will create a compiled regex.