Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex pattern to validate Linux folder path

Using JAVA. I am trying to find a more elegant way for validating a Linux folder path (not including the file name).

What I have so far is this: "^\\/$|^((\\/([a-zA-Z0-9_-]+))+)$"

Folder paths should include only following characters: letters, numbers, dashes or underscore.

Test cases

Valid/ matches:

  • /
  • /abc
  • /abc/abc/abc/abc

Invalid / not-matches:

  • null or empty string
  • /abc/
  • /abc/abc/abc/abc/
like image 747
afrey Avatar asked Mar 08 '19 19:03

afrey


People also ask

What is validate Windows paths?

Validate Windows Paths - Regular Expressions Cookbook, 2nd Edition [Book] 8.18. Validate Windows Paths You want to check whether a string looks like a valid path to a folder or file on the Microsoft Windows operating system.

What is the proper regular expression to match all Unix paths?

The proper regular expression to match all UNIX paths is: 0]+&] That is, one or more characters that are not a NUL.

What is the regex for a non root directory?

Then use this RegEx: any non-root directory, which name is composed out of alphas, numbers, dash or underscore (e.g. /abc/123/_abc-123) What is the most correct regular expression for a UNIX file path?

How to test the regex regex in Java?

Begin ^ and end $ are only needed once (around the two alternatives). You can test the RegEx on RegexPlanet.com (click on Java-Button for tests) Explained: \w matches a word-character (same as [a-zA-Z0-9_], not matching the dash ).


2 Answers

Issue with your RegEx

Your supplied RegEx is working on the test-cases.

You could even reduce it by removing backslashes \\ and outer pair of parentheses. Begin ^ and end $ are only needed once (around the two alternatives).

Possible Solution using Regular Expression

You can test the RegEx on RegexPlanet.com (click on Java-Button for tests)

^/|(/[a-zA-Z0-9_-]+)+$

or equivalent (see demo on RegexPlanet)

^/|(/[\w-]+)+$

Explained: \w matches a word-character (same as [a-zA-Z0-9_], not matching the dash).

Implementation in Java code:

public boolean isValidLinuxDirectory(String path) {
    Pattern linuxDirectoryPattern = Pattern.compile("^/|(/[a-zA-Z0-9_-]+)+$");
     return path != null && !path.trim().isEmpty() && linuxDirectoryPattern.matcher( path ).matches();
}

Alternative Solution using File

Note the docs on isDirectory():

Returns: true if and only if the file denoted by this abstract pathname exists and is a directory; false otherwise

So it may only validate your requirements (valid Linux folder) if run on a Linux machine and if the folder/directory exists.

public boolean isValidExistingDirectory(String path) {
     if (path == null || path.trim().isEmpty()) return false;
     File file = new File( path );
     return file.isDirectory();
}

Extended Solution

As stated in comment the special form of root // should also be valid. Then use this RegEx:

^/|//|(/[\w-]+)+$

It supports:

  1. root-directory /
  2. special form of root-directory //
  3. any non-root directory, which name is composed out of alphas, numbers, dash or underscore (e.g. /abc/123/_abc-123)

See also

  • What is the most correct regular expression for a UNIX file path?
  • Regular expression to validate windows and linux path with extension
  • what is path //, how is it different from /
like image 187
hc_dev Avatar answered Sep 28 '22 05:09

hc_dev


Here ya go: \/[a-zA-Z0-9_\/-]*[^\/]$

EDIT

First character matches a forward slash /. The following character group matches a-z, A-Z, 0-9, underscores, forward slashes, and dashes (all accepted directory and filename characters). The following asterisk makes the pattern match that character group 0 or more times (so any combo of those characters). The last character group has a negation ^ meaning it matches anything EXCEPT what's in the character group, being the final forward slash that we don't want to match. Finally the $ to end the string.

like image 26
Logan Rodie Avatar answered Sep 28 '22 04:09

Logan Rodie