在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)