java提取字符串数字,Java获取字符串中的数字具体的⽅法如下:
/**
* 把字符串数字类型的数字取出来(只取遇到⾮数字字符前,包括空格)
* @param str
* <li>"1-0我5013我24a5c6"    》 1</li>
* <li>"10  5 013我24a 5c6"  》 10</li>
* <li>"105013我24a5c6"      》 105013</li>
* <li>"000"                》 000</li>
* <li>"00010123600"        》 00010123600</li>
* <li>"好20我1a2b"          》空字符串</li>
* @return
*/
public static String getPrefixNumberText(String str){
if(StringUtils.isBlank(str)){
throw new RuntimeException("参数str不能为空");
}
StringBuffer number = new StringBuffer("");
String[] strArray = str.split("");
for (int i=1; i<strArray.length; i++) {
if(RegUtils.isNumberText(strArray[i])){
number.append(strArray[i]);
}else{
break;
}
}
String();
}
/**
* 把字符串数字类型的数字取出来(只取遇到⾮数字字符前,但不包括空格)
* @param str
* <li>"1-0我5013我24a5c6"    》 1</li>
* <li>"10  5 013我24a 5c6"  》 105013</li>
* <li>"105013我24a5c6"      》 105013</li>
* <li>"000"                》 000</li>
* <li>"00010123600"        》 00010123600</li>
* <li>"00010123我600"        》 00010123</li>
* @return
*/
public static String getPrefixNumberTextIgnoreSpace(String str){
if(StringUtils.isBlank(str)){
throw new RuntimeException("参数str不能为空");
}
正则表达式提取中文StringBuffer number = new StringBuffer("");
String[] strArray = str.split("");
for (String string : strArray) {
if(!StringUtils.isBlank(string)){
if(RegUtils.isNumberText(string)){
number.append(string);
}else{
break;
}
}
}
String();
}
/
**
* 把字符串数字类型的所有数字取出来
* @param str
* <li>"1-000我10123我60#0"      》 100010123600</li>
* <li>"00010-+123我600"        》 00010123600</li>
* <li>"我是2019我600"            》 2019600</li>
* <li>"我是20 -19我    600"        》 2019600</li>
* @return
*/
public static String getNumberText(String str){
if(StringUtils.isBlank(str)){
throw new RuntimeException("参数str不能为空");
}
StringBuffer number = new StringBuffer("");
String[] strArray = str.split("");
for (String string : strArray) {
if(!StringUtils.isBlank(string) && RegUtils.isNumberText(string)){
number.append(string);
}
}
String();
}
/
**
* 把字符串数字类型的数字取出来(只取遇到⾮数字字符前,不包括空格)转换成数字
* @param str
* <li>"1-0我5013我24a5c6"    》 1</li>
* <li>"10  5 013我24a 5c6"  》 105013</li>
* <li>"105013我24a5c6"      》 105013</li>
* <li>"000"                》 0</li>
* <li>"00010123600"        》 10123600</li>
* @return
*/
public static long getPrefixNumber(String str){
String number = getPrefixNumberTextIgnoreSpace(str);
if(StringUtils.isBlank(number)){
return 0;
}
//去掉前⾯为0的,如0099变成99
String[] texts = number.split("");
StringBuffer numbers = new StringBuffer("");
for (String text : texts) {
if(numbers.length() < 1){
if(text == "0"){
continue;
}
}
numbers.append(text);
}
if(numbers.length() < 1){
return 0;
}
return Long.String());
}
/**
* 把字符串数字类型的数字取出来转换成数字
* @param str
* <li>"1-000我10123我60#0"  》 100010123600</li>
* <li>"00010-+123我600"      》 10123600</li>
* <li>"我是2019我600"        》 2019600</li>
* <li>"我是20 -19我    600"    》 2019600</li>
* @return
*/
public static long getNumber(String str){
String number = getNumberText(str);
if(StringUtils.isBlank(number)){
return 0;
}
//去掉前⾯为0的,如0099变成99
String[] texts = number.split("");
StringBuffer numbers = new StringBuffer("");
for (String text : texts) {
if(numbers.length() < 1){
if(text == "0"){
continue;
}
}
numbers.append(text);
}
if(numbers.length() < 1){
return 0;
}
return Long.String());
}
正则表达式⼯具类:
import Matcher;
import Pattern;
import org.apachemons.lang.StringUtils;
/**
* 正则表达式⼯具类
*
*/
public class RegUtils {
/**
* 邮箱
*/
public static final String EMAIL = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
/**
* ⼿机号码
*/
public static final String PHONE = "^(1[3-9]([0-9]{9}))$";
/**
* 仅中⽂
*/
public static final String CHINESE = "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$";
/**
* 整数
*/
public static final String INTEGER = "^-?[1-9]\\d*$";
/**
* 数字
*/
public static final String NUMBER = "^([+-]?)\\d*\\.?\\d+$";
/**
* 正整数
*/
public static final String INTEGER_POS = "^[1-9]\\d*$";
/**
* 浮点数
*/
public static final String FLOAT = "^([+-]?)\\d*\\.\\d+$";
/**
* 正浮点数
*/
public static final String FLOAT_POS = "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$";
/**
* 是否为正整数数字,包括0(00,01⾮数字)
*/
public static final String INTEGER_WITH_ZERO_POS = "^(([0-9])|([1-9]([0-9]+)))$";
/**
* 是否为整数数字,包括正、负整数,包括0(00,01⾮数字)
*/
public static final String NUMBER_WITH_ZERO = "^((-)?(([0-9])|([1-9]([0-9]+))))$";
/
**
* 是否为数字字符串
*/
public static final String NUMBER_TEXT = "^([0-9]+)$";
/**
* 数字(整数、0、浮点数),可以判断是否⾦额,也可以是负数
*/
public static final String NUMBER_ALL = "^((-)?(([0-9])|([1-9][0-9]+))(\\.([0-9]+))?)$";
/**
* QQ,5-14位
*/
public static final String QQ = "^[1-9][0-9]{4,13}$";
/**
* IP地址
*/
public static final String IP = "((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))";
/**
* 邮编
*/
public static final String POST_CODE = "[1-9]\\d{5}(?!\\d)";
/**
* 普通⽇期
*/
public static final String DATE = "^[1-9]\\d{3}-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2][0-9])|(3[0-1]))$";
/**
* 复杂⽇期,不区分闰年的2⽉
* ⽇期格式:2017-10-19
* 或2017/10/19
* 或2017.10.19
* 或2017年10⽉19⽇
* 最⼤31天的⽉份:(((01|03|05|07|08|10|12))-((0[1-9])|([1-2][0-9])|(3[0-1])))
* 最⼤30天的⽉份:(((04|06|11))-((0[1-9])|([1-2][0-9])|(30)))
* 最⼤29天的⽉份:(02-((0[1-9])|([1-2][0-9])))
*/
public static final String DATE_COMPLEX = "^(([1-2]\\d{3})(-|/|.|年)((((01|03|05|07|08|10|12))(-|/|.|⽉)((0[1-9])|([1-2][0-9])|(3[0-1])))|(((04|06|11))(-|/|.|⽉)((0[1-9])|([1-2][0-9])|(30)))|(02-((0[1-9])|([1-2][0-9]))))(⽇)?)$";
/**
* 复杂的⽇期,区分闰年的2⽉
* 这个⽇期校验能区分闰年的2⽉,格式如下:2017-10-19
* (见:www.jb51/article/50905.htm)
* ^((?!0000)[0-9]{4}-((0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])-(29|30)|(0[13578]|1[02])-31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)$
*/
public static final String DATE_COMPLEX_LEAP_YEAR = "^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:
0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$"    /**
* 正则表达式校验,符合返回True
* @param regex 正则表达式
* @param content 校验的内容
* @return
*/
public static boolean isMatch(String regex, CharSequence content){
return Pattern.matches(regex, content);
}
/**
* 校验⼿机号码
* @param mobile
* @return
*/
public static final boolean isMoblie(String mobile){
boolean flag = false;
if (null != mobile && !im().equals("") && im().length() == 11) {
Pattern pattern = Patternpile(PHONE);
Matcher matcher = pattern.im());
flag = matcher.matches();
}
return flag;
}
/**
* 校验邮箱
* @param value
* @return
*/
public static final boolean isEmail(String value){
boolean flag = false;
if (null != value && !im().equals("")) {
Pattern pattern = Patternpile(EMAIL);
Matcher matcher = pattern.im());
flag = matcher.matches();
}
return flag;
}
/**
* 校验密码
* @param password
* @return 长度符合返回true,否则为false
*/
public static final boolean isPassword(String password){
boolean flag = false;
if (null != password && !im().equals("")) {
password = im();
if(password.length() >= 6 && password.length() <= 30){
return true;
}
}
return flag;
}
/**
* 校验⼿机验证码
* @param value
* @return 符合正则表达式返回true,否则返回false
*/
public static final boolean isPhoneValidateCode(String value){
boolean flag = false;
if (null != value && !im().equals("")) {
Pattern pattern = Patternpile("^8\\d{5}$");
Matcher matcher = pattern.im());
flag = matcher.matches();
}
return flag;
}
/
**
* 判断是否全部⼤写字母
* @param str
* @return
*/
public static boolean isUpperCase(String str){
if(StringUtils.isEmpty(str)){
return false;
}
String reg = "^[A-Z]$";
return isMatch(reg,str);
}
/**
* 判断是否全部⼩写字母
* @param str
* @return
*/
public static boolean isLowercase(String str){
if(StringUtils.isEmpty(str)){
return false;
}
String reg = "^[a-z]$";
return isMatch(reg,str);
}
/**
* 是否ip地址
* @param str
* @return
*/
public static boolean isIP(String str){
if(StringUtils.isEmpty(str)){
return false;
}
return isMatch(IP, str);
}
/**
* 符合返回true,区分30、31天和闰年的2⽉份(最严格的校验),格式为2017-10-19    * @param str
* @return
*/
public static boolean isDate(String str){
if(StringUtils.isEmpty(str)){
return false;
}
return isMatch(DATE_COMPLEX_LEAP_YEAR, str);
}
/**
* 简单⽇期校验,不那么严格
* @param str
* @return
*/
public static boolean isDateSimple(String str){
if(StringUtils.isEmpty(str)){
return false;
}
return isMatch(DATE, str);
}
/**
* 区分30、31天,但没有区分闰年的2⽉份
* @param str
* @return
*/
public static boolean isDateComplex(String str){
if(StringUtils.isEmpty(str)){
return false;
}
return isMatch(DATE_COMPLEX, str);
}
/**
* 判断是否为数字字符串,如0011,10101,01
* @param str
* @return
*/
public static boolean isNumberText(String str){
if(StringUtils.isEmpty(str)){
return false;
}
return isMatch(NUMBER_TEXT, str);
}
/**
* 判断所有类型的数字,数字(整数、0、浮点数),可以判断是否⾦额,也可以是负数    * @param str
* @return
*/
public static boolean isNumberAll(String str){
if(StringUtils.isEmpty(str)){
return false;
}
return isMatch(NUMBER_ALL, str);
}
/
**
* 是否为正整数数字,包括0(00,01⾮数字)
* @param str
* @return
*/
public static boolean isIntegerWithZeroPos(String str){
if(StringUtils.isEmpty(str)){
return false;
}
return isMatch(INTEGER_WITH_ZERO_POS, str);
}
/
**
* 是否为整数,包括正、负整数,包括0(00,01⾮数字)
* @param str
* @return
*/
public static boolean isIntegerWithZero(String str){
if(StringUtils.isEmpty(str)){
return false;
}
return isMatch(NUMBER_WITH_ZERO, str);
}
/
**
* 符合返回true,QQ,5-14位
* @param str
* @return
*/
public static boolean isQQ(String str){
if(StringUtils.isEmpty(str)){
return false;
}
return isMatch(QQ, str);
}
public static void main(String[] args) {
System.out.println(isMoblie("134********"));
System.out.println(isMoblie("177********"));
System.out.println(isMoblie("176********"));
System.out.println(isMoblie("14730800244"));
System.out.println(isMoblie("183********"));
System.out.println(isMoblie("19330800244"));
System.out.println(isMoblie("1333000244"));
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。