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();
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.