Regular expression to validate windows and linux path with extension
Asked Answered
R

2

2

I am trying to write a function which will validate weather the given path is valid in Linux/Windows with file extension.

ex:

Windows path: D:\DATA\My_Project\01_07_03_061418738709443.doc
Linux path: /source_data/files/08_05_09_1418738709443.pdf

The code that I have tried is

static String REMOTE_LOCATION_WIN_PATTERN = "([a-zA-Z]:)?(\\\\[a-z  A-Z0-9_.-]+)+.(txt|gif|jpg|png|jpeg|pdf|doc|docx|xls|xlsx|DMS)\\\\?";

static String REMOTE_LOCATION_LINUX_PATTERN = "^(/[^/]*)+.(txt|gif|jpg|png|jpeg|pdf|doc|docx|xls|xlsx|DMS)/?$";

public boolean checkPathValidity(String filePath) {

   Pattern linux_pattern = Pattern.compile(REMOTE_LOCATION_LINUX_PATTERN);
   Pattern win_pattern = Pattern.compile(REMOTE_LOCATION_WIN_PATTERN);
   Matcher m1 = linux_pattern.matcher(filePath);
   Matcher m2 = win_pattern.matcher(filePath);

   if (m1.matches() || m2.matches()) {
      return true;
   } else {
      return false;
   }
}

This function gives result true if path is valid in either windows/linux. The above function is not returning right result for some of the paths that contain dates, _ ? , * in their path.

Reaganreagen answered 31/5, 2017 at 15:38 Comment(2)
Is this even worth doing? You’ll get an exception on the remote side, whether the file has invalid characters, or the file has valid characters but refers to a nonexistent or inaccessible path. For instance, "/root/08_05_09.pdf" would pass a regex check, but probably would fail whatever operation your application is doing. So there will be a check for validity on the remote side regardless.Tepic
These validation will get executed on client side itself. the I am just configuring the checkPathValidity() method according to remote side.Reaganreagen
C
2
static String REMOTE_LOCATION_WIN_PATTERN = "([a-zA-Z]:)?(\\\\[a-z  A-Z0-9_.-]+)+.(txt|gif|jpg|png|jpeg|pdf|doc|docx|xls|xlsx|DMS)\\\\?";

static String REMOTE_LOCATION_LINUX_PATTERN = "^(/[^/]*)+.(txt|gif|jpg|png|jpeg|pdf|doc|docx|xls|xlsx|DMS)/?$";

static Pattern linux_pattern = Pattern.compile(REMOTE_LOCATION_LINUX_PATTERN);
static Pattern win_pattern = Pattern.compile(REMOTE_LOCATION_WIN_PATTERN);

static final boolean WINDOWS = System.getProperty("os.name").startsWith("Windows");


public boolean checkPathValidity(String filePath) {
   Matcher m = WINDOWS ? win_pattern.matcher(filePath) : linux_pattern.matcher(filePath);

   return m.matches();    
}
Chian answered 31/5, 2017 at 16:15 Comment(2)
I have tried path G:\DATA\GREEN_DOC\MyCab\index\16_12_06_05_09_1418738709443F.DOC . It's not workingReaganreagen
I know that is what my question is about. I need a regular expression that will validate the file paths on windows and linux systemReaganreagen
D
0

You can basically combine the two patterns as follows:

  1. windows: [a-zA-Z]:\\(?:([^<>:"\/\\|?*]*[^<>:"\/\\|?*.]\\|..\\)*([^<>:"\/\\|?*]*[^<>:"\/\\|?*.]\\?|..\\))?
  2. linux: \/.*
  3. total: (\/.*|[a-zA-Z]:\\(?:([^<>:"\/\\|?*]*[^<>:"\/\\|?*.]\\|..\\)*([^<>:"\/\\|?*]*[^<>:"\/\\|?*.]\\?|..\\))?)

This works fine on my angular input tag pattern validation.

Check the ling for more details How would I match a pattern for both windows and Linux directory path?

Dafna answered 28/7, 2021 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.