0

I need to create regular expression that would validate path to file. It should approve such strings like:

c:\
c:\dir1\file.txt
c:\dir1\dir2\file.txt

and so on. I tried to create it.Result:

(c|C):(\\\w{0,8})*(\.\w{1,3})?

In gskinner everything OK, but when I compile this pattern in Java none of the previous rows are not tested.

Java code:

p = Pattern.compile("(c|C):(\\\w{0,8})*");
m = p.matcher(arguments);
result = m.matches();
Ibrahim Najjar
  • 19,178
  • 4
  • 69
  • 95
Raman Branavitski
  • 864
  • 1
  • 9
  • 22
  • Show us what you have already. – GaryF Nov 05 '13 at 08:54
  • 1
    Did you escape the `\\` when you wrote the pattern as a Java string? – Henry Nov 05 '13 at 08:54
  • My code in java p = Pattern.compile("(c|C):(\\\w{0,8})*"); m = p.matcher(arguments); result = m.matches(); – Raman Branavitski Nov 05 '13 at 08:56
  • 1
    This should be 4 backslashes, not three. – Henry Nov 05 '13 at 09:03
  • It is not that easy as it looks. Path can be absolute or relative, can contain drive letter or not, may be UNC path, does not have to use backslashes but may use slashes (on windows too). File may have an extension or not, extnesion does not have to be 3 char wide. Path may also contain /../ or /./. Path can contain brackets, dashes, underscores (not only characters) etc ...... – Artur Nov 05 '13 at 09:07
  • why 4? The first and second mean "\", \w mean any leter – Raman Branavitski Nov 05 '13 at 09:09
  • @user2462686 This is java, not perl. See my explanation. For example, you need "\\w" in your regex, not "\w". No idea why I was downvoted. – Steve P. Nov 05 '13 at 09:17
  • Every backslash in Java String must be backslashed so for your regex to contain '\w' your string must have '\\w'. Use Pattern.toString() to verify how your regex really looks like to Java. – Artur Nov 05 '13 at 09:19
  • 1
    @user2462686: BTW - first rule - before you paste any code - verify compiler's output. Java compiler will not allow you to place 3 consecutive backslashes in a String. It will not even compile. – Artur Nov 05 '13 at 09:22

1 Answers1

0

I just edited your example code to work:

String regex = "[cC]:(?:\\\\\\w{0,8})*(?:[.]\\w{1,3})?"

The regular expression \\ matches a single backslash since \ is a special character in regex, and hence must be escaped, but once we put this in quotes, aka turn it into a String, we need to escape each of the backslashes, yielding "\\\\". We need the additional two \\ for w.

Steve P.
  • 14,489
  • 8
  • 42
  • 72