Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regex to find particular file

Tags:

java

regex

In one of my test case I want to copy particular jar of a component from one location to another location. e.g when target directory has only following jars

org.test.custom.search-4.2.2-SNAPSHOT-.jar
org.test.custom.search-4.2.2-tests-SNAPSHOT.jar

I want to copy the org.test.custom.search-4.2.2-SNAPSHOT-.jar . Where version of this jar can be changed at any time . So I can use the regex for that purpose as mentioned here[1]. But I want to know how to omit the other jar in regex. i.e want to omit the jar which has string "tests" in its name.

1.Regex for files in a directory

like image 891
Jenananthan Avatar asked Jul 29 '16 08:07

Jenananthan


2 Answers

You can use indexOf instead of regex to check if the file name containing the word "tests" like this:

if(fileName.indexOf("tests") >= 0) {
    // do what you want
}

Update: indexOf() will be much quicker than a regex, and is probably also easier to understand.

like image 102
Bahramdun Adil Avatar answered Nov 15 '22 08:11

Bahramdun Adil


The regex based solution would be:

if (fileName.matches(".*tests.*")) {
    //do something
} else {
    //do something else
}
like image 26
xenteros Avatar answered Nov 15 '22 07:11

xenteros