java提取字符串数字,Java获取字符串中的数字

具体的方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
     * 把字符串数字类型的数字取出来(只取遇到非数字字符前,包括空格)
     * @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;
            }
        }
        return number.toString();
    }
     
     
    /**
     * 把字符串数字类型的数字取出来(只取遇到非数字字符前,但不包括空格)
     * @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;
                }
            }
        }
        return number.toString();
    }
     
     
    /**
     * 把字符串数字类型的所有数字取出来
     * @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);
            }
        }
        return number.toString();
    }
     
     
    /**
     * 把字符串数字类型的数字取出来(只取遇到非数字字符前,不包括空格)转换成数字
     * @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.parseLong(numbers.toString());
    }
     
     
    /**
     * 把字符串数字类型的数字取出来转换成数字
     * @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.parseLong(numbers.toString());
    }

正则表达式工具类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import org.apache.commons.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
     * (见:http://www.jb51.net/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 && !mobile.trim().equals("") && mobile.trim().length() == 11) {
            Pattern pattern = Pattern.compile(PHONE);
            Matcher matcher = pattern.matcher(mobile.trim());
            flag = matcher.matches();
        }
        return flag;
    }
     
     
    /**
     * 校验邮箱
     * @param value
     * @return
     */
    public static final boolean isEmail(String value){
        boolean flag = false;
        if (null != value && !value.trim().equals("")) {
            Pattern pattern = Pattern.compile(EMAIL);
            Matcher matcher = pattern.matcher(value.trim());
            flag = matcher.matches();
        }
        return flag;
    }
     
     
    /**
     * 校验密码
     * @param password
     * @return 长度符合返回true,否则为false
     */
    public static final boolean isPassword(String password){
        boolean flag = false;
        if (null != password && !password.trim().equals("")) {
            password = password.trim();
            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 && !value.trim().equals("")) {
            Pattern pattern = Pattern.compile("^8\\d{5}$");
            Matcher matcher = pattern.matcher(value.trim());
            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("13430800244"));
        System.out.println(isMoblie("17730800244"));
        System.out.println(isMoblie("17630800244"));
        System.out.println(isMoblie("14730800244"));
        System.out.println(isMoblie("18330800244"));
        System.out.println(isMoblie("19330800244"));
        System.out.println(isMoblie("1333000244"));
    }
     
 
}

  

posted @   Ning-  阅读(1532)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结
点击右上角即可分享
微信分享提示