java Java中文件扩展名的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38018941/
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-11-03 03:02:22  来源:igfitidea点击:

Regular expression for file extensions in Java

javaregex

提问by MohanVS

I am trying to write a simple regular expression to identify all filenames in a list which end with ".req.copied" extension. The code I am using is given below

我正在尝试编写一个简单的正则表达式来标识列表中以“.req.copied”扩展名结尾的所有文件名。我正在使用的代码如下

public class Regextest {
 public static void main(String[] args) {
    // TODO Auto-generated method stub
 String test1=new String("abcd.req.copied");
  if(test1.matches("(req.copied)?")) {
     System.out.println("Matches");
    }
  else
     System.out.println("Does not Match");
    }

 }

The regex tests ok in online regex testers but does not function in the program. I have tried multiple combinations (like splitting req and copied into two regexes, or literal matching of the dot character) but nothing works (even the simplest regex of (reg)? returned a "Does not Match" output). Please let me know how to tackle this.

正则表达式在在线正则表达式测试器中测试正常,但在程序中不起作用。我尝试了多种组合(例如将 req 拆分并复制到两个正则表达式中,或点字符的字面匹配)但没有任何效果(即使是最简单的 (reg) 正则表达式?返回“不匹配”输出)。请让我知道如何解决这个问题。

采纳答案by Pshemo

Main problem with matcheshere is that it requires from regex to match entirestring. But in your case your regex describes only partof it.

matches这里的主要问题是它需要从正则表达式匹配整个字符串。但是在您的情况下,您的正则表达式仅描述了其中的一部分

If you really want to use matcheshere your code could look more like

如果你真的想在matches这里使用你的代码可能看起来更像

test1.matches(".*\.req\.copied")
  • .represents any character (except line separators like \r) so if you want it to represent only dot you need to escape it like \.(in string we need to write it as "\\."because \has also special meaning there - like creating special characters \r\n\tand so on - so it also requires escaping via additional \).
  • .*will let regex accept any characters before .req.copied
  • .代表任何字符(除了像 那样的行分隔符\r)所以如果你想让它只代表点,你需要像这样转义它\.(在字符串中我们需要把它写成"\\."因为\那里也有特殊的含义——比如创建特殊字符\r\n\t等等——所以它还需要通过额外的\) 进行转义。
  • .*会让正则表达式接受任何字符之前 .req.copied

But in your case you should simply use endsWithmethod

但在你的情况下,你应该简单地使用endsWith方法

test1.endsWith(".req.copied")

回答by user8397947

As resueman said in the comments, you don't need a regex for that. You can simply check if each filename endsWith(".req.copied").

正如 resueman 在评论中所说,您不需要正则表达式。您可以简单地检查每个文件名endsWith(".req.copied").

if(test1.endsWith(".req.copied")){
    System.out.println("Matches");
}else{
    System.out.println("Does not match");
}

By the way, the above if-else can be replaced with System.out.println(test1.endsWith(".req.copied") ? "Matches" : "Does not match");.

顺便说一下,上面的 if-else 可以替换为System.out.println(test1.endsWith(".req.copied") ? "Matches" : "Does not match");.

回答by sinclair

test1.matches(".*\\.req\\.copied")should do it but in your case you should consider using endsWith()instead of matches.

test1.matches(".*\\.req\\.copied")应该这样做,但在您的情况下,您应该考虑使用endsWith()而不是matches.

回答by everton

You should come up with a Regex that would match the whole string format, not a snippet:

你应该想出一个匹配整个字符串格式的正则表达式,而不是一个片段:

String test1= "abcd.req.copied";

if(test1.matches("^.*req\.copied$")) {
    System.out.println("Matches");
} else {
    System.out.println("Does not Match");
}

Also, your format was using (req.copied)?, which would match any case. Also, .symbol matches any character, so escape it for matching a dot.

此外,您的格式正在使用(req.copied)?,这将匹配任何情况。此外,.符号匹配任何字符,因此将其转义以匹配点。