blue-sky
blue-sky

Reputation: 53876

return jar file name

A regular expression to return logging.jar. Path can contain varying levels of depthness.

I think I need to use the '.' operator but am unsure how to handle varying amount of slashes ?

Both below test cases should return logging.jar

C:/proj/test/tester/base/logging.jar C:/proj/test/tester/base/leveldeeper/logging.jar

Upvotes: 1

Views: 1472

Answers (3)

Sunil Kumar B M
Sunil Kumar B M

Reputation: 2795

Solution without regular expression

String path = "C:/proj/test/tester/base/leveldeeper/logging.jar";
String splitPath[] = path.split("/");

System.out.println("Jar File name: " + splitPath[splitPath.length - 1]);

Upvotes: 1

shift66
shift66

Reputation: 11958

.*/([^\\.]*)\\..*
in group 1 you'll get the name of the file.extension doesn't matter. If you want to get name only for jar files use this pattern:
.*/([^\\.]*)\\.jar

.* is a greedy regex and it will match until the last /.After / there is only file's name with extension and you just need to get it's name. [^\\.]* is greedy too and will match until the last dot (.), in fact what you need because the last dot is the extension separator.

Upvotes: 0

Alexander Pavlov
Alexander Pavlov

Reputation: 32296

/([^/]+\.jar)$/ will give you the result in the first capturing group.

Upvotes: 1

Related Questions