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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4489582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:59:09  来源:igfitidea点击:

java regular expression to match file path

javaregex

提问by Jijoy

I was trying out to create a regular expression to match file path in java like

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

C:\abc\def\ghi\abc.txt

C:\abc\def\ghi\abc.txt

I tried this ([a-zA-Z]:)?(\\[a-zA-Z0-9_-]+)+\\?, like following code

我试过这个([a-zA-Z]:)?(\\[a-zA-Z0-9_-]+)+\\?,就像下面的代码

import java.util.regex.Pattern;

  public class RETester {

public static void main(String arhs[]){

    String regularExpression = "([a-zA-Z]:)?(\[a-zA-Z0-9_-]+)+\?";

    String path = "D:\directoryname\testing\abc.txt";

    Pattern pattern = Pattern.compile(regularExpression);

    boolean isMatched = Pattern.matches(regularExpression,path);
    System.out.println(path);
    System.out.println(pattern.pattern());
    System.out.println(isMatched);

}

}

However it's always giving me , false as result . Pls help me .

然而,它总是给我 , false 结果。请帮助我 。

Thanks

谢谢

采纳答案by falstro

Java is using backslash-escaping too, you know, so you need to escape your backslashes twice, once for the Java string, and once for the regexp.

Java 也在使用反斜杠转义,您知道,因此您需要对反斜杠进行两次转义,一次用于 Java 字符串,一次用于正则表达式。

"([a-zA-Z]:)?(\\[a-zA-Z0-9_.-]+)+\\?"

Your regexp matched a literal '[-zA-Z0-9_-' string, and a literal '?' at the end. I also added a period in there to allow 'abc.txt'..

您的正则表达式匹配文字 '[-zA-Z0-9_-' 字符串和文字 '?' 在末尾。我还在那里添加了一个句点以允许'abc.txt'..

That said, consider using another mechanism for determine valid file names, as there are different schemes (i.e. unix). java.util.File will probably throw an exception if the path is invalid, which might be a good alternative, although I don't like using exceptions for control flow...

也就是说,考虑使用另一种机制来确定有效的文件名,因为有不同的方案(即 unix)。如果路径无效,java.util.File 可能会抛出异常,这可能是一个不错的选择,尽管我不喜欢将异常用于控制流......

回答by Ralph

It does not match, because your regex match only to paths, not to files. -- More correct: it does not accept the dotin your file name.

它不匹配,因为您的正则表达式仅匹配路径,而不匹配文件。-- 更正确:它不接受文件名中的

And in addition, there is the escaping problem mentiond by roe.

另外还有roe提到的逃逸问题。

回答by darioo

Use this regex:

使用这个正则表达式:

"([a-zA-Z]:)?(\\[a-zA-Z0-9._-]+)+\\?";

I added two modifications: you forgot to add .for matching the file name abc.txtand backslash escaping (\\) was also needed.

我添加了两个修改:您忘记添加.以匹配文件名,abc.txt并且\\还需要反斜杠转义 ( )。

回答by Artur

There are two reasons why it is giving you false. First one is that you need \\\\instead of \\because you need to escape these characters. And the second one is that you're missing a dot character, you can insert it before a-zas ([a-zA-Z]:)?(\\\\[.a-zA-Z0-9_-]+)+\\\\?

它给你错误的原因有两个。第一个是你需要\\\\而不是\\因为你需要转义这些字符。而第二个是,你缺少一个点字符,你可以前插入它a-z作为([a-zA-Z]:)?(\\\\[.a-zA-Z0-9_-]+)+\\\\?

回答by finbrein

Just saying, one should replace the .in

只是说,应该替换.in

([a-zA-Z]:)?(\\[a-zA-Z0-9_.-]+)+\\?

with \\.

\\.

.is meant for any character in a regular expression (Java style), while
\.is specifically meant for . character, and we need to escape the backslash

.用于正则表达式(Java 风格)中的任何字符,而
\.专门用于 . 字符,我们需要转义反斜杠

回答by To Kra

Here is correct regex for windows filesystem:

这是 Windows 文件系统的正确正则表达式:

Regular Expression:

正则表达式:

(?:[a-zA-Z]\:)\([\w-]+\)*\w([\w-.])+  

as a Java string

作为 Java 字符串

"(?:[a-zA-Z]\:)\\([\w-]+\\)*\w([\w-.])+"

回答by iammyr

If it has to match only the path of files lying on the same machine where your app is running, then you can use:

如果它只需要匹配运行应用程序的同一台机器上的文件路径,那么您可以使用:

try{
    java.nio.file.Paths.get(yourPath);
}(catch InvalidPathException err){
}

So if you're running your app on windows the code above will catch invalid windows paths and if you're running on unix, it will catch invalid unix paths, etc.

因此,如果您在 Windows 上运行您的应用程序,上面的代码将捕获无效的 Windows 路径,如果您在 unix 上运行,它将捕获无效的 unix 路径等。

回答by Cjo

Since the path contains folders and folder name can contain any character other than

由于路径包含文件夹,文件夹名称可以包含除

? \ / : " * < >

? \ / : " * < >

We can use the below regex to match a directory path [it uses all the symbols that a folder name can afford]

我们可以使用下面的正则表达式来匹配目录路径[它使用文件夹名称可以承受的所有符号]

[A-Za-z]:[A-Za-z0-9\!\@\#$\%\^\&\(\)\'\;\{\}\[\]\=\+\-\_\~\`\.\]+