英文:
Java regular expression returning false on match
问题
我基本上在尝试筛选以 .ack.gz 结尾的文件名。
例如,String filename = "somefile.ack.gz";
return filename.matches(".ack.gz$"); 返回 false
return filename.matches("\\.ack\\.gz$"); 返回 false
难道这两个中的一个不应该匹配吗?不确定我在这里漏掉了什么...
英文:
I am basically trying to filter file names that end with .ack.gz
e.g. String filename = "somefile.ack.gz"
return filename.matches(".ack.gz$"); returns false
return filename.matches("\\.ack\\.gz$"); returns false
Shouldn't one of these match? Not sure what I have missed here...
答案1
得分: 1
String.matches
方法 (javadoc) 用于测试正则表达式是否与整个目标字符串匹配。
但在你的示例中,正则表达式 ".ack.gz$"
并不匹配整个 "somefile.ack.gz"
字符串的所有字符。因此 match
返回 false
。
如果你真的想要使用正则表达式来实现这一点,那么你的代码应该是:
return filename.matches(".*\\.ack\\.gz");
注意:
.*
匹配文件名的第一个部分。\\.
表示匹配字面上的.
字符。如果不进行转义,.
将匹配任意单个字符。双重转义是必需的,因为模式被表示为 Java 字符串字面量。- 前导的
^
和结尾的$
是不必要的。它们由match
的语义隐含表示。
但正如 @kaya 指出的那样,在这里最好使用 String.endsWith
(javadoc),因为你不需要正则表达式功能来进行这个测试。
英文:
The String.matches
method (javadoc) tests to see if the regex matches the entire target string.
But in your example, the regex ".ack.gz$"
does not match all of the characters of "somefile.ack.gz"
. Therefore match returns false
.
If you really want to use a regex to do this, then your code should be:
return filename.matches(".*\\.ack\\.gz");
Notes:
- The
.*
matches the first part of the filename. - The
\\.
says to match a literal.
character. If you have no escaping then.
matches any single character. A double escape is required because the pattern is being expressed as a Java string literal. - Leading
^
and trailing$
are unnecessary. They are implied by the semantics ofmatch
.
But as @kaya points out, it would be better to use String.endsWith
(javadoc) here since you don't need regex functionality for this test.
答案2
得分: 0
Matches
返回true
,如果指定的正则表达式与整个目标字符串匹配。如果表达式只与字符串的一部分匹配,它不会返回true
。因此,要使用matches
,您需要提供一个与整个文件名匹配的表达式。在您的情况下,应该是:
return filename.matches(".*\\.ack\\.gz"); // 返回 false
表达式开头的.*
与字符串开头的任何内容匹配,只要它不被表达式的其余部分匹配,这样表达式就会与整个目标字符串匹配。
您不需要使用$
,因为表达式已经必须要与字符串的末尾匹配,所以它是多余的。
英文:
Matches
returns true
the specified regular expression matches the entire target string. It won't return true
if the expression only matches part of the string. So to use matches
, you want to supply an expression that matches the whole filename. In your case, that would be:
return filename.matches(".*\\.ack\\.gz"); returns false
The .*
at the front of the expression matches anything at the front of the string that isn't matched by the rest of the expression, so that the expression will match the entire target string.
You don't need the $
because the expression already has to match to the end of the string, and so it's redundant.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论