Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中匹配文件路径的正则表达式_Java_Regex - Fatal编程技术网

在java中匹配文件路径的正则表达式

在java中匹配文件路径的正则表达式,java,regex,Java,Regex,可能重复: 我试图创建一个正则表达式来匹配java中的文件路径 比如C:/WINDOWS/Profiles/myComputer/Desktop/test.xml 请帮帮我 非常感谢您可以用这样的东西 "([a-zA-Z]:)?(\\\\[a-zA-Z0-9_.-]+)+\\\\?" 下面是一个适用于您的案例的示例。也许您需要添加一些不允许的字符,但这只是在[\w\s.]中添加字符。您可以试试这个 (?:[\w]\:|\\)(\\[a-z_\-\s0-9\.]+)+\.(?i)(txt|x

可能重复:

我试图创建一个正则表达式来匹配java中的文件路径

比如
C:/WINDOWS/Profiles/myComputer/Desktop/test.xml

请帮帮我


非常感谢

您可以用这样的东西

"([a-zA-Z]:)?(\\\\[a-zA-Z0-9_.-]+)+\\\\?"
下面是一个适用于您的案例的示例。也许您需要添加一些不允许的字符,但这只是在[\w\s.]中添加字符。您可以试试这个

 (?:[\w]\:|\\)(\\[a-z_\-\s0-9\.]+)+\.(?i)(txt|xml|gif|pdf|doc|docx|xls|xlsx)$
说明:

^(?:[\w]\:|\\) -- Begin with x:\ or \\
[a-z_\-\s0-9\.] -- valid characters are a-z| 0-9|-|.|_ (you can add more)
(?i) -- regular expression case-insensitive
(txt|xml|gif|pdf|doc|docx|xls|xlsx) -- Valid extension (you can add more)

需要正则表达式吗?此外,这会给您带来麻烦-允许的字符依赖于系统;值得注意的是,有File.separator,有些系统允许:,有些系统不允许。@see。
^(?:[\w]\:|\\) -- Begin with x:\ or \\
[a-z_\-\s0-9\.] -- valid characters are a-z| 0-9|-|.|_ (you can add more)
(?i) -- regular expression case-insensitive
(txt|xml|gif|pdf|doc|docx|xls|xlsx) -- Valid extension (you can add more)