Maestros
Maestros

Reputation: 419

Regex to match "path/*.extension"

I am trying to find a regular expression that would match the following format:

path/*.file_extension

For example:

temp/*.jpg
usr/*.pdf
var/lib/myLib.so
tmp/

Using the regex, I want to store the matching parts into a String array, such as:

String[] tokens;
// regex magic here
String path = tokens[0];
String filename = tokens[1];
String extension = tokens[2];

In case of the last case tmp/, that contains no filename and extension, then token[1] and token[2] would be null. In case of the: usr/*.pdf then the token[1] would contain only the string "*".

Thank you very much for your help.

Upvotes: 1

Views: 3506

Answers (5)

Yogendra Singh
Yogendra Singh

Reputation: 34367

On a different approach, a simple usage of 'substring()/lastIndexOf()' methods should serve the purpose:

    String filePath = "var/lib/myLib.so";
    String fileName = filePath.substring(filePath.lastIndexOf('/')+1);
    String path = filePath.substring(0, filePath.lastIndexOf('/'));
    String fileName = fileName.substring(0, fileName.lastIndexOf('.'));
    String extension = fileName.substring(fileName.lastIndexOf('.')+1);

Please Note: You need to handle the alternate scenarios e.g. file path without extension.

Upvotes: 0

Mike Samuel
Mike Samuel

Reputation: 120486

Why use a regular expression? I personally find lastIndexOf more readable.

String path;
String filename;
@Nullable String extension;

// Look for the last slash
int lastSlash = fullPath.lastIndexOf('/');
// Look for the last dot after the last slash
int lastDot = fullPath.lastIndexOf('.', lastSlash + 1);
if (lastDot < 0) {
  filename = fullPath.substring(lastSlash + 1);
  // If there is no dot, then there is no extension which
  // is distinct from the empty extension in "foo/bar."
  extension = null;
} else {
  filename = fullPath.substring(lastSlash + 1, lastDot);
  extension = fullPath.substring(lastDot + 1); 
}

Upvotes: 0

Pshemo
Pshemo

Reputation: 124215

If you can use Java7 then you can use named groups like this

String data = "temp/*.jpg, usr/*.pdf, var/lib/*.so, tmp/*, usr/*, usr/*.*";

Pattern p = Pattern
        .compile("(?<path>(\\w+/)+)((?<name>\\w+|[*]))?([.](?<extension>\\w+|[*]))?");

Matcher m = p.matcher(data);
while (m.find()) {
    System.out.println("data=" + m.group());
    System.out.println("path=" + m.group("path"));
    System.out.println("name=" + m.group("name"));
    System.out.println("extension=" + m.group("extension"));
    System.out.println("------------");
}

Upvotes: 2

Jim Scarborough
Jim Scarborough

Reputation: 123

I'm assuming you're using Java. This should work:

Pattern.compile("path/(.*?)(?:\\.(file_extension))?");

Upvotes: 0

anubhava
anubhava

Reputation: 784898

This code should wotk:

String line = "var/lib/myLib.so";
Pattern p = Pattern.compile("(.+?(?=/[^/]*$))/([^.]+)\\.(.+)$");
Matcher m = p.matcher(line);
List<String> tokens = new ArrayList<String>();
if (m.find()) {
    for (int i=1; i <= m.groupCount(); i++) {
        tokens.add(m.group(i));
    }
}
System.out.println("Tokens => " + tokens);

OUTPUT:

Tokens => [var/lib, myLib, so]

Upvotes: 0

Related Questions