pmc*_*521 1 java regex string digit
我正在写一个算法,我需要检查一个字符串是否只包含一个数字(不超过一个).目前我有:
if(current_Operation.matches("\\d")){
...
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做这个?谢谢.
您可以使用:
^\\D*\\d\\D*$
# match beginning of the line
# non digits - \D*
# one digit - \d
# non digits - \D*
# end of the line $
Run Code Online (Sandbox Code Playgroud)
请参阅regex101.com上的演示(为了清晰起见,添加了新行).