Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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 用于检查字符串是否以[0-8]结尾的正则表达式返回false_Java_Regex - Fatal编程技术网

Java 用于检查字符串是否以[0-8]结尾的正则表达式返回false

Java 用于检查字符串是否以[0-8]结尾的正则表达式返回false,java,regex,Java,Regex,我需要一个正则表达式来检查字符串是否以0到8之间的数字结尾。为什么这个正则表达式返回false String time = "" + new Date().getTime();// 1394205719160 if(time.endsWith("[0-8]"))//false 方法String.endsWith不接受正则表达式, 它将您的输入按字面意思处理为字符串[0-8] 与regex[0-8]不同,您可以尝试以下方法: if (time.matches(".*[0-8]$")) 或者可能

我需要一个正则表达式来检查字符串是否以0到8之间的数字结尾。为什么这个正则表达式返回false

String time = "" + new Date().getTime();// 1394205719160
if(time.endsWith("[0-8]"))//false

方法
String.endsWith
不接受正则表达式,
它将您的输入按字面意思处理为字符串
[0-8]


与regex
[0-8]
不同,您可以尝试以下方法:

if (time.matches(".*[0-8]$"))
或者可能更有效:

char lastChar = time.charAt(time.length() - 1);
if (lastChar >= '0' && lastChar <= '8') { ... }
char lastChar=time.charAt(time.length()-1);

if(lastChar>='0'&&lastChar@code578841441似乎其他人已经提供了备选方案。endsWith检查字符串匹配,而不是正则表达式匹配。有关备选方案,请参阅答案。我不太确定对此问题投反对票的原因。OP提供了当前代码、预期输出和实际输出。