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
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.
The regex
based solution would be:
if (fileName.matches(".*tests.*")) {
//do something
} else {
//do something else
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With