[英]I need to write a string that contains only numbers from 0 to 9
So i have this project where i need to write a mutator method that changes a person's phone number only if the phone number contains numbers from 0 to 9 and no letters or any other things. 因此,在这个项目中,我需要编写一个mutator方法,仅当电话号码包含从0到9的数字且没有字母或其他任何东西时,该方法才能更改一个人的电话号码。 The type of the phone number is string and i tried using conditional statements.
电话号码的类型是字符串,我尝试使用条件语句。 If the mobile contains anything other than numbers then nothing should happen.
如果移动电话包含数字以外的任何内容,则什么也不会发生。 this is what i got so far:
这是我到目前为止所得到的:
public void setMobile(String mobile) {
if(mobile.matches("[a-zA-z]+")){
}
else{
this.mobile = mobile;
}
}
thanks 谢谢
您的正则表达式模式应为[0-9]+
If you are looking for anything that isn't a number then the pattern is \\D
. 如果您要查找的不是数字,则模式为
\\D
That just means non-digit and is a shorthand for [^0-9]
那只是意味着非数字,是
[^0-9]
的简写
See http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html 参见http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.