Ashraf Sadaqat
Ashraf Sadaqat

Reputation: 23

Regular expressions ignoring a file extension

I need some help with a regular expression. I have the following 4 file names

heapdump.20160406.214053.18914.0013.phd heapdump.20160406.214053.18914.0013.phd.gz javacore.20160406.214053.18914.0002.txt javacore.20160406.214053.18914.0002.txt.gz

Basically what I need is for my regular expression to ignore the files with the .gz on the end of it. I tried the following but it does not seem to work.

/heapdump.*.phd|javacore.*.txt/i
/heapdump*.phd|javacore*.txt/i
/heapdump.\d+.\d+.\d+.\d+.phd|javacore.\d+.\d+.\d+.\d+.txt/i

Thanks

Upvotes: 2

Views: 236

Answers (3)

rock321987
rock321987

Reputation: 11032

This will work

(?!.*\.gz$)(^.*$)

Regex Demo

JS Code

var re = /(?!.*\.gz$)(^.*$)/gm; 
var str = 'heapdump.20160406.214053.18914.0013.phd\nheapdump.20160406.214053.18914.0013.phd.gz\njavacore.20160406.214053.18914.0002.txt\njavacore.20160406.214053.18914.0002.txt.gz';

var result = str.match(re);
document.writeln(result)

Upvotes: 3

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520898

One option which does not require using a regular expression would be to split the filename on period (.) into an array, and then check if the last element of the array contains the extension gz:

var filename = "heapdump.20160406.214053.18914.0013.phd.gz";
var parts = filename.split(".");

if (parts[parts.length - 1] == 'gz') {
    alert("ignore this file");
}
else {
    alert("pay attention to this file");
}

Upvotes: 0

Francesco
Francesco

Reputation: 4250

It depends on how much you want the solution to be precise. If you only have phd and txt extensions this will work

/heapdump.*\.(phd|txt)$/

Which means: a string starting with heapdump, followed by whatever, then a dot, then phd or txt, end of line

Or you can simply negate a string that ends with dot gz

/.*\.gz$/

Upvotes: 0

Related Questions