Regex to strip all directorynames from Path (leave filename)

You don't need a regex. Just find the last slash and use substring:

int index = path.lastIndexOf(File.separatorChar);
String name = path.substring(index+1);

or use:

new File(path).getName();

This covers all spectrums directories, trailing or starting slashes.

All others here so far does not...

public static String extractFilename(String path)  {  
    java.util.regex.Pattern p       = java.util.regex.Pattern.compile('^[/\\\\]?(?:.+[/\\\\]+?)?(.+?)[/\\\\]?$');
    java.util.regex.Matcher matcher = p.matcher(path);

    if ( matcher.find() ) {
        return matcher.group(1);
    }
    return null;
}

Used:

println extractFilename("data\\\\path/to/file/RandomFile.pdf")
println extractFilename("RandomFile.pdf")
println extractFilename("RandomFile.pdf/")
println extractFilename("data\\\\path/to/file/RandomFile.pdf/")
println extractFilename("/data\\\\path/to/file/RandomFile.pdf/")
println extractFilename("/data\\\\path/to/file/RandomFile.pdf")
println extractFilename("/RandomFile.pdf")
println extractFilename("/RandomFile.pdf/")
println extractFilename("/")

Prints

RandomFile.pdf
RandomFile.pdf
RandomFile.pdf
RandomFile.pdf
RandomFile.pdf
RandomFile.pdf
RandomFile.pdf
RandomFile.pdf
/

.......................................................................EDIT............................................................................

Explanation for Uday. It was actually pretty complicated one, and I am not sure I can argue for all of it today, but I will give it a try :)

^[/\\\\]?(?:.+[/\\\\]+?)?(.+?)[/\\\\]?$

0: Entire regex

^

1: Starts with

[/\\\\]?

2: A forward slash or backward slash ( yes, four slashes for one, crazy! ). Once or not at all, so not required.

(?:.+[/\\\\]+?)? 

3: This step is the complicated one. It is intended to skip everything but the last one that matches this exact pattern, a non capturing group (?:... were we are looking for any character several times, followed by one slash.

The group can be repeated many times, but it is non greedy. So it is saying do this, except until you match the following regex explained in 4.

This entire piece though, is not required, because of the ? outside the parentheses. For instance, "/RandomFile.pdf/" will not generate a match here, and continue with 4.

However, now I do find this a bit weird, since .+ is greedy, still it is looking forward to the slash for the match. It might be the nature of groups, that they are non-greedy or a bug in Java pattern syntax.

(.+?)[/\\\\]?$

4: Since the regex applies for all of the string, it also has to match up to the end. The previous match at 3 was non greedy, reluctant using +?, meaning it will only match as long as the regex after it doesn't also match. Our word is at the end $ is within the parentheses which may or may not end with a slash. I have chosen to return the root path as the file name if there is no filename, but just a slash, since it is also a filename ( directory name )

5: The parentheses is a capturing group, which is what we return at the end.

I hope this clarifies a bit.


Try with this:

new File("Payload/brownie.app/Info.plist").getName()

This returns the filename without directories.

Example:

String filename = new File("Payload/brownie.app/Info.plist").getName();
System.out.println(filename);

Outupt:

Info.plist

Use replace with regex, String name = directory.replaceAll(".*/",""), simple as that.

Tags:

Java

String

Regex