3

I am developing an application a where user need to supply local file location or remote file location. I have to do some validation on this file location.
Below is the requirement to validate the file location.

Path doesn't contain special characters * | " < > ?.
And path like "c:" is also not valid.

Paths like

  • c:\,
  • c:\newfolder,
  • \\casdfhn\share

are valid while

  • c:
  • non,
  • \\casfdhn

are not.

I have implemented the code based on this requirement:

String FILE_LOCATION_PATTERN = "^(?:[\\w]\\:(\\[a-z_\\-\\s0-9\\.]+)*)";
String REMOTE_LOCATION_PATTERN = "\\\\[a-z_\\-\\s0-9\\.]+(\\[a-z_\\-\\s0-9\\.]+)+";

Pattern locationPattern = Pattern.compile(FILE_LOCATION_PATTERN);
Matcher locationMatcher = locationPattern.matcher(iAddress);
if (locationMatcher.matches()) {
    return true;
}

locationPattern = Pattern.compile(REMOTE_LOCATION_PATTERN);
locationMatcher = locationPattern.matcher(iAddress);

return locationMatcher.matches();

Test:

worklocation'        pass
'C:\dsrasr'          didnt pass  (but should pass)
'C:\saefase\are'     didnt pass  (but should pass)
'\\asfd\sadfasf'     didnt pass  (but should pass)
'\\asfdas'           didnt pass  (but should not pass)
'\\'                 didnt pass  (but should not pass)
'C:'                 passed infact should not pass

I tried many regular expression but didn't satisfy the requirement. I am looking for help for this requirement.

MrA
  • 529
  • 3
  • 7
  • 18

2 Answers2

4

The following should work:

([A-Z|a-z]:\\[^*|"<>?\n]*)|(\\\\.*?\\.*)

The lines highlighted in green and red are those that passed. The non-highlighted lines failed.

Bear in mind the regex above is not escaped for java

enter image description here

user184994
  • 16,798
  • 1
  • 38
  • 48
1

from your restrictions this seems very simple.

^(C:)?(\\[^\\"|^<>?\\s]*)+$

Starts with C:\ or slash ^(C:)?\\

and can have anything other than those special characters for the rest ([^\\"|^<>?\\s\\\])*

and matches the whole path $

Edit: seems C:/ and / were just examples. to allow anything/anything use this:

^([^\\"|^<>?\\s])*(\\[^\\"|^<>?\\s\\\]*)+$

Adam Yost
  • 3,534
  • 21
  • 36
  • its not necessary to have C: it can be anything – MrA Jun 12 '14 at 19:28
  • I also tried this I am getting error: java.util.regex.PatternSyntaxException: Unmatched closing ')' near index 27 ^([^"|^<>?\s])*\([^"|^<>?\s])*$ ^ – MrA Jun 12 '14 at 19:47
  • I also tried mine on internet it works but didnt in java using pattern class and matcher – MrA Jun 12 '14 at 19:50
  • in java did you guys make sure to double escape? the "\" need to be "\\" in java. Edited the answer to use double escapes – Adam Yost Jun 12 '14 at 19:56
  • yeah i made that changes.."^([^\\\"|^<>?\\s])*\\\\([^\\\"|^<>?\\s])*$".. it works fine.. thanks – MrA Jun 12 '14 at 19:58
  • but how can i avoid : "\\asdfas" and "\\" this should not pass – MrA Jun 12 '14 at 20:00
  • too many escapes. just 2, you have 3. if you want no double slashes (which are a valid filepath component btw) I will edit it as such. – Adam Yost Jun 12 '14 at 20:02
  • valid path is c:\, c:\newfolder, \\casdfhn\share – MrA Jun 12 '14 at 20:05