views:

56

answers:

1

Hi all. Have a perl script that needs to process all files of a certain type from a given directory. The files should be those that end in .jup, and SHOULDN'T contain the word 'TEMP_' in the filename. I.E. It should allow corrected.jup, but not TEMP_corrected.jup.

Have tried a look-ahead, but obviously have the pattern incorrect:

/(?!TEMP_).*\.jup$/

This returns the entire directory contents though, including files with any extension and those containing TEMP_, such as the file TEMP_corrected.jup.

Fairly new to 'lookarounds' obviously, any help greatly appreciated!

+2  A: 

The regular expression you want is:

/^((?!TEMP_).)*\.jup$/

The main difference is that your regular expression is not anchored at the start of the string, so it matches any substring that satisfies your criteria - so in the example of TEMP_corrected.jup, the substrings corrected.jup and EMP_corrected.jup both match.

(The other difference is that putting () round both the lookahead and the . ensures that TEMP_ isn't allowed anywhere in the string, as opposed to just not at the start. Not sure whether that's important to you or not!)

If you're getting files other than .jup files, then there is another problem - your expression should only match .jup files. You can test your expression with:

perl -ne 'if(/^((?!TEMP_).)*\.jup$/) {print;}'

then type strings: perl will echo them back if they match, and not if they don't. For example:

$ perl -ne 'if(/^((?!TEMP_).)*\.jup$/) {print;}'
foo
foo.jup
foo.jup              <-- perl printed this because 'foo.jup' matched
TEMP_foo.jup
psmears
Yes, perfect thanks for the answer and the explanation!
pmmenneg