[英]Checking a number with regular expressions in Java
I have a number "8756342536"
. 我有一个数字
"8756342536"
。 I want to check whether the number starts with "8"
, contains 10 digits all of it is numeric, using regular expression. 我想使用正则表达式检查数字是否以
"8"
开头,是否全部包含10位数字。
What pattern would I need for this scenario in Java? 在Java中,此方案需要什么模式? Thanks in advance.
提前致谢。
使用String的matchs(...)方法:
boolean b = "8756342536".matches("8\\d{9}");
Use this expression: 使用以下表达式:
^8\d{9}$
meaning an 8 followed by exactly 9 digits. 意思是8后跟正好9位数。 So for example:
因此,例如:
String number = ...
if (number.matches("^8\\d{9}$")) {
// it's a number
}
Given the number is in String
form: 给定数字为
String
形式:
String number = "8756342536";
boolean match = number.matches ("^[8][0-9]{9}$");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.