我正在使用下面的正则表达式:
Pattern testPattern= Pattern.compile("^[1-9][0-9]{14}");
Matcher teststring= testPattern.matcher(number);
if(!teststring.matches())
{
error("blah blah!");
}
Run Code Online (Sandbox Code Playgroud)
我的要求是:
我在正则表达式中遗漏了什么?
Roh*_*ain 18
与"^[1-9][0-9]{14}"你匹配的15数字,而不是10-15数字.{14}量词将完全匹配14先前模式的重复.使用{m,n}量词给出一个范围:
"[1-9][0-9]{9,14}"
Run Code Online (Sandbox Code Playgroud)
你并不需要使用锚与Matcher#matches()方法.锚是隐含的.在这里你可以直接使用String#matches()方法:
if(!teststring.matches("[1-9][0-9]{9,14}")) {
// blah! blah! blah!
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
52810 次 |
| 最近记录: |