我正在编写一个算法,我需要检查字符串是否包含only one数字(不超过一位)。目前我有:
if(current_Operation.matches("\\d")){
...
}
有更好的方法来做这件事吗?谢谢。
您可以使用:
^\\D*\\d\\D*$
# match beginning of the line
# non digits - \D*
# one digit - \d
# non digits - \D*
# end of the line $
See regex101.com 上的演示 https://regex101.com/r/a0ulwZ/1(为了清晰起见添加了换行符)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)