Java regular match Windows file directory (Detailed)

Late-night blog (> v0)

Recent experimental class to use Java to write Socket programming, file server to do a similar kind of FTP, use the command line to ls, cd, get files, the way encountered a problem, how to match the Windows file directory is being used, such as D:\CSDN\我的博客a string like in turn online for a long time looking to find there is no ready-made, the result is not very comprehensive expression, forget or write your own regular expressions it, then wrote a very comprehensive sense is an expression of for your reference, there are problems you can review the comments section, I was doing modifications


This only applies to the regular expression used in Java, and applies only to the absolute file path match of Windows, if you want to reuse, you can take a look at ideas, delete some of the tasteless places

I use a String of matches ways to do regular match, String This method is very sad, can only judge whether the string complies with regular expressions, and there are some very silent bug

First gave me the code segment, in explaining to do:

String text = "D:\\CSDN\\我的博客";
String fileRegex = "^[A-z]:\\\\([^|><?*\":\\/]*\\\\)*([^|><?*\":\\/]*)?$";
System.out.println(text.matches(fileRegex));

Regular expressions here is this ^[A-z]:\\\\([^|><?*\":\\/]*\\\\)*([^|><?*\":\\/]*)?$, saw that it was a bit complicated, is actually very simple

First, the D:\\CSDN\\我的博客reason why the \\, because here to escape, or else you will eclipse syntax error;
then we look at this expression, we divide it into three parts would be more clear to see:
1 ^[A-z]:\\\\, 2 ([^|><?*\":\\/]*\\\\)*, 3([^|><?*\":\\/]*)?$

  • The first paragraph is to match the absolute path to the top of the letter of the name, for example D:\, you might be wondering, why this is not a \but three, that's what I think this method bug place Normally, regular expressions really only with a write \on it, but there need three \to do an escape, an escape that's different from the 2 [pattern] in
  • The second paragraph of the match is D:\the path after the address, for example CSDN\, where regular means: In addition to matching |><?*":\/outside these characters are not allowed in the Windows file plus all the characters \of zero or more times, so here contains the English and other common characters, the equivalent of doing a process of elimination; need to be reminded that, where []there is \only one \can escape
  • The third paragraph is made a special treatment, any special deal with it? When there is a situation, if you do not write the third paragraph will be a problem, for example D:\CSDN\我的博客, can only be matched to the second paragraph 文件夹+\, the last folder If no \, you can not match; In order to deal with this situation, we wrote a third section; the third paragraph is meant to match the name of only one folder at the end of the string zero or one; if finally there \, the first two matches to take effect; if no final \is based on the first two, the third also entered into force
Published 65 original articles · won praise 58 · Views 100,000 +

Guess you like

Origin blog.csdn.net/AngelLover2017/article/details/84147705