繁体   English   中英

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

[英]Regex for checking if string ends with [0-8] returns false

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

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

您可以尝试以下方法:

if (time.matches(".*[0-8]$"))

或更有效:

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

方法String.endsWith不接受正则表达式,
它将您的输入从字面上视为字符串[0-8]
不作为正则表达式[0-8]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM