mac
2024-08-22 30859ca8f2175475d2c666353bc27f3b2ceede53
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
package com.zxing.utils;
 
import android.annotation.SuppressLint;
import android.text.TextUtils;
 
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * 正则表达式
 *
 */
@SuppressLint("WrongConstant")
public class Validator {
 
    public static boolean password(String password) {
        return password.matches("[0-9a-zA-Z]{8,16}") && !password.matches("[0-9]+") && !password.matches("[a-zA-Z]+");
    }
 
    public static List<String> findMac(String src) {
        List<String> list = new ArrayList<String>();
        if (src == null || "".equals(src))
            return list;
        Pattern pattern = Pattern
                .compile("[0-9a-z]{2}:[0-9a-z]{2}:[0-9a-z]{2}:[0-9a-z]{2}:[0-9a-z]{2}:[0-9a-z]{2}");
        Matcher matcher = pattern.matcher(src);
        while (matcher.find()) {
            list.add(matcher.group(0));
        }
        return list;
    }
 
    public static List<String> findColor(String src) {
        List<String> list = new ArrayList<String>();
        if (src == null || src.equals(""))
            return list;
        Pattern pattern = Pattern
                .compile("#[0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8}");
        Matcher matcher = pattern.matcher(src);
        while (matcher.find()) {
            list.add(matcher.group(0));
        }
        return list;
    }
 
    /**
     * 去除汉字,归正编码
     */
    public static String replaceHanzi(String input) {
        if (TextUtils.isEmpty(input)) {
            return "";
        }
 
        /**
         * 归正编码
         */
        byte[] bytes = input.getBytes();
        String info = "";
 
        for (int i = 0; i < bytes.length; i++) {
            if (bytes[i] < 0) {
                bytes[i] = 32;
            }
            info = info + new String(new byte[]{bytes[i]});
        }
 
        /**
         * 去除中文
         */
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(info);
        List<String> inputs = new ArrayList<>();
 
        if (m.find()) {
            for (int i = 0; i < info.length(); i++) {
                String ever = info.substring(i, i + 1);
                Matcher m1 = p.matcher(ever);
                if (m1.find()) {
                    ever = "";
                }
                inputs.add(ever);
            }
 
            String inputNew = "";
            for (int i = 0; i < inputs.size(); i++) {
                inputNew = inputNew + inputs.get(i);
            }
            return inputNew.trim();
 
        }
        return info.trim();
    }
 
    /**
     * 验证邮箱
     */
    public static boolean checkEmail(String email) {
        boolean flag;
        try {
            String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            Pattern regex = Pattern.compile(check);
            Matcher matcher = regex.matcher(email);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }
 
    /**
     * 获得汉语拼音首字母
     */
    public static String getAlpha(String str) {
        if (str == null) {
            return "#";
        }
 
        if (str.trim().length() == 0) {
            return "#";
        }
 
        char c = str.trim().substring(0, 1).charAt(0);
        // 正则表达式,判断首字母是否是英文字母
        Pattern pattern = Pattern.compile("^[A-Za-z]+$");
        if (pattern.matcher(c + "").matches()) {
            return (c + "").toUpperCase();
        } else {
            return "#";
        }
    }
 
    /**
     * 校验URL
     */
    public static boolean checkUrl(String url) {
        if (TextUtils.isEmpty(url)) return false;
        boolean flag;
        try {
            String check = "^((https|http|ftp|rtsp|mms|axd):\\/\\/)[^\\s]+";
            Pattern regex = Pattern.compile(check, Pattern.CASE_INSENSITIVE);
            Matcher matcher = regex.matcher(url.replaceAll(" ", ""));
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
 
        return flag;
    }
 
    /**
     * 校验图片URL
     */
    public static boolean checkImageUrl(String url) {
        if (TextUtils.isEmpty(url)) return false;
        boolean flag;
        try {
            String check = "^((https|http):\\/\\/)[^\\s]+.(png|jpg|gif|webp)";
            Pattern regex = Pattern.compile(check, Pattern.CASE_INSENSITIVE);
            Matcher matcher = regex.matcher(url);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
 
        return flag;
    }
 
    /**
     * 校验正整数
     */
    public static boolean checkInt(String intContent) {
        if (TextUtils.isEmpty(intContent)) return false;
        boolean flag;
        try {
            String check = "^[1-9]\\d*$";
            Pattern regex = Pattern.compile(check, Pattern.CASE_INSENSITIVE);
            Matcher matcher = regex.matcher(intContent);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
 
        return flag;
    }
 
    public static boolean checkColor(String color) {
        if (TextUtils.isEmpty(color))
            return false;
        boolean flag;
        try {
            String check = "^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$";
            Pattern regex = Pattern.compile(check, Pattern.CASE_INSENSITIVE);
            Matcher matcher = regex.matcher(color);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }
}