简体   繁体   中英

Regex to validate file path with leading double backslash in Java

I have a file path as below: \\ppk-ccr-02-llp\MOP_CIP\CO_PROJECT\alpha\stage\GOOGLE.COM_PROJECTS\bolt_cyclic_84\mango_test\8267

i want a regex to validate this in such a way that it checks

  1. the string always starts with double backslash
  2. followed by alpha numeric
  3. rest of the single backslash is always surrounded with alpha numeric
  4. it only allows _. \ - as special characters

The following expression \\\\([-.\w]+\\)*[-.\w]+ should work as shown in the demo:

  • \\\\([-.\w]+\\)*[-.\w]+ - starting with \\
  • ([-.\w]+\\)* - followed by 0 or else non-empty "words" consisting of alphanumerics, . , - and ending with \
  • [-.\w]+ - entire string ending with non-empty "word"

In Java code, the backslashes are additionally escaped, so the path is validated as:

String fullPath = "\\\\ppk-ccr-02-llp\\MOP_CIP\\CO_PROJECT\\alpha\\stage\\GOOGLE.COM_PROJECTS\\bolt_cyclic_84\\mango_test\\8267";

if (fullPath.matches("\\\\\\\\([-.\\w]+\\\\)*[-.\\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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM