java - Regex to match "path/*.extension" -
i trying find regular expression match following format:
path/*.file_extension
for example:
temp/*.jpg usr/*.pdf var/lib/mylib.so tmp/
using regex, want store matching parts string array, such as:
string[] tokens; // regex magic here string path = tokens[0]; string filename = tokens[1]; string extension = tokens[2];
in case of last case tmp/
, contains no filename , extension, token[1] , token[2] null. in case of the: usr/*.pdf
token[1] contain string "*"
.
thank help.
if can use java7 can use named groups 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("------------"); }
Comments
Post a Comment