Java Regex for file path validation

Viewed 91

I need to come up with a regex according to these rules. It is guaranteed that path is either a correct path, or it has some of the following errors:

More than one ~

~ is not at the start

~ mixed with \ (~ in Windows path)

More than one C:

C: is not at the start

C: mixed with / (C: in Unix path)

\ mixed with /

Illegal paths example:

/folder1/folder2\folder3

C:\User/root

/dev/~/

C:/a/b/c/d

~\folder

~/~

~~

C:\Folder\Subfolder\C:\

So far i used "([A-Z|a-z]:|~|/|\\[^|"<>?\n])|(\\\\.?\\.)" and "([a-zA-Z]:)?(\\[a-zA-Z0-9_.-]+)+\\?"

its a method that accepts String path and boolean toWin(if we need to convert unix path to windows). /////////// What i need to do --> Method signature: public static String convertPath(String path, boolean toWin) Convert path to Unix\Windows path depending on a boolean parameter. Unix path may start with ~ or /. Every subdirectory must end with / character except the last one. . and .. refer to current directory and parent directory. Filename doesn't necessarily have the extension. // i managed to solve it, but cant come up with a regex to validate path

I would really appreciate some help. Thanks.

0 Answers
Related