8

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

1
  • You could use something to filter files with a stream? Commented Jul 29, 2016 at 8:02

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

This will filter both files at the moment. Modifying to "-tests-" would solve the problem
Sir if you see it is 'tests' not 'test'. Anyway the way I show is OK.
Where in your answer is regex which the question is about?
In question says: 'want to omit the jar which has string "tests" in its name' and also my answer is exactly is to solve this problem.
The title is Java regex to find.
|
1

The regex based solution would be:

if (fileName.matches(".*tests.*")) {
    //do something
} else {
    //do something else
}

Comments

0

Matches the SNAPSHOT exactly with any given version number, ignoring all others (including tests-SNAPSHOT):

org\.test\.custom\.search-\d+\.\d+\.\d+-SNAPSHOT-\.jar

Comments

-1

One regex to match the main jar but not the test jar could be:

\w+-[\d.]+-(?!tests-).*\.jar

It has a negative matcher for the string "tests-". Note that you'll have to escape the backslashes when you put this regex into a string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.