开发者

Java - regular expression to split directory paths

I am trying to do the following in Java

give a directory path like "/a/b/c" I want to get a array of string as ["a", "b", "c"]. The code is as follows :

private static final String DIRECTORY_PATH_SEPARATOR = "/";  
    Iterator iter = testPaths.iterator();

            String[] directories;

        while( iter.hasNext() ) {

      开发者_运维百科      directories = ( ( String ) iter.next() ).split(DIRECTORY_PATH_SEPARATOR);
        }

but what i get as array is space as well. I want to get all those strings with length>0.

how can I do that??


The only place you'd get an empty string (in a valid path) would be the first item if the path started with the separator. If it had the leading separator, split on the path without it.

String path = "/a/b/c";
String[] directories = path.substring(1).split(DIRECTORY_PATH_SEPARATOR);
// { "a", "b", "c" }

As noted by OmnipotentEntity, my assumption was wrong about valid paths. You'd otherwise have to go through the array and keep nonempty strings when using split().

String path = "/a/b////c";
String[] split = path.split(DIRECTORY_PATH_SEPARATOR);
ArrayList<String> directories = new ArrayList<String>(split.length);
for (String dir : split)
    if (dir.length() > 0)
        directories.add(dir);

An alternative is to use actual regular expressions to match non-separator characters:

String path = "/a/b////c";
ArrayList<String> directories = new ArrayList<String>();
Pattern regex = Pattern.compile("[^" + DIRECTORY_PATH_SEPARATOR + "]+");
Matcher matcher = regex.matcher(path);
while (matcher.find())
    directories.add(matcher.group());


You should use the File class for this, not a regex.


The String.split(String) method is not very flexible, and its results can be surprising (for example silently discarding empty trailing strings). I am using the Guava libraries (Google Core Libraries for Java) for all of my projects, and that contains a very nice Splitter utility that does what you want in a very readable and predictable way:

String path = "/a/b///c///";
Iterable<String> directories = Splitter
    .on(DIRECTORY_PATH_SEPARATOR)
    .omitEmptyStrings()
    .split(path);

That gives you an iterable that you can directly use in a for loop like this:

for(String directory : directories) {
  System.out.println(directory);
}

In that case it doesn't even create an intermediate object to store the directories, but it creates them on the fly.

If you do need the directory list, you can copy the iterable into an ArrayList or array like this:

List<String> dirList = Lists.newArrayList(directories);
String[] dirArray = Iterables.toArray(directories, String.class);

As a closing note: For writing portable code you should use File.separator instead of DIRECTORY_PATH_SEPARATOR.


Here's a more reliable non-regex version that uses file system routines:

public static List<String> splitFilePath(final File f){
    if(f == null){
        throw new NullPointerException();
    }
    final List<String> result = new ArrayList<String>();
    File temp = f.getAbsoluteFile();
    while(temp != null){
        result.add(0, temp.getName());
        temp = temp.getParentFile();
    }
    return result;
}

Test code:

public static void main(final String[] args){
    final File f = new File("foo/bar/phleem.txt");
    final List<String> parts = splitFilePath(f);
    System.out.println(parts);
}

Output:

[, home, seanizer, projects, eclipse, helios2, stackfiddler, foo, bar, phleem.txt]

I return a List because it's more usable, but there's nothing to stop you from either changing the method to return an array or converting the result from outside using this:

List<String> parts = splitFilePath(f);
String[] partsAsArray = parts.toArray(new String[parts.size()]);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜