Administrator
2022-09-14 58d006e05dcf2a20d0ec5367dd03d66a61db6849
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
 
package com.mandi.common.ParamFileter;
 
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import com.mandi.dao.common.ObjectResult;
 
 
 
/** 
 * 正则表达式验证 <br/> 
 * 2016年4月10日 下午4:37:21 <br/> 
 * @author   guolq        
 */
public class RegexCheck {
    
    
    /** 
     * checkphone:验证手机号码11位
     * @param phone
     * @return 
     * TODO
     */  
    public static boolean checkphone(String phone){
        boolean flag = false; 
        if(phone==null||phone.trim().isEmpty())
            return flag;
        try{ 
            Pattern p = Pattern.compile("^[1][0-9]{10}$"); //^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$
            Matcher m = p.matcher(phone);
            flag = m.matches(); 
        }catch(Exception e){ 
            flag = false; 
        } 
            return flag;
    }
    
    /** 
     * checkempty:字符串不能为空和null
     * @param strargs
     * @return 
     * TODO
     */  
    public static boolean checkempty(String...strargs){
        for (String str : strargs) {
            if (str==null||str.trim().isEmpty()) {
                return false;
            }
        }
        return true;
    }
    public static <T> String checkempty(T obj,String[][] fieldandname){
        StringBuffer bf=new StringBuffer();
        if(obj==null)
        {
            bf.append("对象不存在!");
            return bf.toString();
        }
        @SuppressWarnings("rawtypes")
        Class cls=obj.getClass();
        try {
            if(fieldandname==null||fieldandname[0].length<=0)
                return bf.toString();
            for(int i=0;i<fieldandname[0].length;i++)
            {
                String fields=fieldandname[0][i];
                Field prop=cls.getDeclaredField(fields);
                prop.setAccessible(true);
                if(prop.get(obj)==null||prop.get(obj).toString().trim().isEmpty())
                {
                    if(bf.toString().length()!=0)
                        bf.append(","+fieldandname[1][i]);
                    else
                        bf.append(fieldandname[1][i]);
                }
            }
            if(bf.length()!=0)
                bf.append("  为必填项不能为空!");
        } catch (Exception e) {
            e.printStackTrace();
            bf.setLength(0);
            bf.append("必填项不能为空获取出错!");
            return bf.toString();
        }
        return bf.toString();
    }
    
    /** 
     * 复制相同对象
     * @param reobj 返回对象
     * @param orobj 源对象
     * @param fiedsName 字段名数组
     * @return 
     */  
    public static <T> T copyobj(T reobj,T orobj,String[] fiedsName){
        if(reobj==null)
            return null;
        if(orobj==null||fiedsName==null||fiedsName.length<=0)
            return reobj;
        @SuppressWarnings("rawtypes")
        Class recls=reobj.getClass();
        @SuppressWarnings("rawtypes")
        Class orcls=orobj.getClass();
        for(int i=0;i<fiedsName.length;i++)
        {
            String fname=fiedsName[i];
            try {
                Field orprop = orcls.getDeclaredField(fname);
                Field reprop=recls.getDeclaredField(fname);
                if(orprop==null||reprop==null)
                    continue;
                orprop.setAccessible(true);
                reprop.setAccessible(true);
                reprop.set(reobj, orprop.get(orobj));
            } catch (Exception e) {
                e.printStackTrace();
            } 
        }
        return reobj;
    }
    public static <T> Map<String, Object> ObjtoMap(T Obj,String[] fiedsName){
        Map<String, Object> mp=new HashMap<String, Object>();
        if(Obj==null)
            return mp;
        @SuppressWarnings("rawtypes")
        Class recls=Obj.getClass();
        if(fiedsName==null||fiedsName.length<=0){
            Field[] ff=recls.getFields();
            for (int i = 0; i < ff.length; i++) {
                
//                mp.put(, );
            }
        }else
        {
            for(int i=0;i<fiedsName.length;i++)
            {
                String fname=fiedsName[i];
                try {
//                    Field orprop = orcls.getDeclaredField(fname);
                    Field reprop=recls.getDeclaredField(fname);
//                    if(orprop==null||reprop==null)
//                        continue;
//                    orprop.setAccessible(true);
                    reprop.setAccessible(true);
//                    reprop.set(Obj, orprop.get(orobj));
                } catch (Exception e) {
                    e.printStackTrace();
                } 
            }
        }
        
        return null;
    }
    
    
    /** 
     * checkpassword:验证密码某些格式
     * @param pwd
     * @param oldpwd
     * @return 
     * TODO
     */  
    public static ObjectResult<Boolean> checkpassword(String pwd,String oldpwd){
        ObjectResult<Boolean> objresult =new ObjectResult<Boolean>();
        try {
            if(oldpwd==null||pwd==null||oldpwd.trim().isEmpty()||pwd.trim().isEmpty()){
                objresult.setCode(1);
                objresult.setHint("密码不能设置为空");
                objresult.setItem(false);
                return objresult;
            }
            //pwd=pwd.replaceAll("\\s*|\t|\r|\n| |    |", "").replaceAll(" | ", "");//去除字符串中所包含的空格(包括:空格(半角)、制表符、换页符等) 
            Pattern p = Pattern.compile("(?!^\\d+$)(?!^[a-zA-Z]+$).{6,16}"); 
            Matcher m = p.matcher(pwd);
            boolean boo = m.matches(); 
            if(boo){
                objresult.setCode(0);
                objresult.setHint(pwd);
                objresult.setItem(boo);
            }else{
                objresult.setCode(1);
                objresult.setHint("密码格式必须包含:字母和数字(可以包含特殊字符)的6到16位的字符串");
                objresult.setItem(boo);
            }
        } catch (Exception e) {
            objresult.setCode(1);
            objresult.setHint("密码检验出错,请重新输入密码!!");
        }
        return objresult;    
    }
    
    
    /** 
     * getsearchwords:简单处理分割一些搜索词
     * @param shword
     * @return 
     * TODO
     */  
    public static String getsearchwords(String shword){
        
        if (shword==null||shword.trim().isEmpty()) 
            return null;
        if (shword.length()>200) {
            shword=shword.substring(0, 200);//200个字符
        }
        
        shword=shword.replaceAll(" |,|,|,|    |    ", " ");//替换一些特殊字符
        shword=shword.replaceAll("&nbsp{2,}", " ");
        String[] strs=shword.trim().split(" ");
        if (strs==null) 
            return null;
        StringBuffer strbuf=new StringBuffer();
        for (int i =0; i<strs.length; i++) {
            if (strs[i].trim().isEmpty()) 
                continue;
            strbuf.append(strs[i]+",");
        }
        return strbuf.toString();
        
    }
    
    public static boolean isIP(String ipaddress){
            String ip = "((([1-9]?|1\\d)\\d|2([0-4]\\d|5[0-5]))\\.){3}(([1-9]?|1\\d)\\d|2([0-4]\\d|5[0-5]))";   
           Pattern pattern = Pattern.compile(ip);   
           Matcher matcher = pattern.matcher(ipaddress);   
           return matcher.matches();   
    }
    
    /** 
     * getRadm:生成任意位数的随机字符串
     * @param panflag
     * @param randomnum
     * @return 
     * TODO
     */  
    public static String  getRadm(int panflag,int randomnum){
        char[] chars =new char[]{'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
        StringBuffer result =new StringBuffer();
        if(panflag==1){
            for (int i = 0; i < randomnum ; i++) {
                int indx=(int)(Math.random()*340);
                indx=indx%34;
                result.append(chars[indx]);
            }
        }else if(panflag==2){
            for (int i = 0; i < randomnum ; i++) {
                int indx=(int)(Math.random()*580);
                indx=indx%58;
                result.append(chars[indx]);
            }
        }else if(panflag==3){
            for (int i = 0; i < randomnum ; i++) {
                int indx=(int)(Math.random()*100);
                indx=indx%10;
                result.append(chars[indx]);
            }
        }
        return result.toString();
    }
    
    /** 
     * repllusername:字符串替换姓名 匿名
     * @param username
     * @return 
     * TODO
     */  
    public static String repllusername(String username){
        if (username==null||username.trim().isEmpty()) {
            return "y***"+9;
        }
        if (username.length()<=1) {
            return username+"***"+9;
        }
        if (username.length()==2) {
            return username.substring(0,1)+"***"+username.substring(1);
        }
        return username.substring(0,1)+"***"+username.substring(username.length()-1);
    }
    
    public static boolean isVehicleID(String vehicleID)
    {
        boolean boo=false;
        if(vehicleID!=null&&!vehicleID.trim().isEmpty())
        {
            //拖拉机
            String regex="";
            if(vehicleID.length()==7){
                regex="[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领无]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$";
//                regex="[\u4e00-\u9fa5]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$";
            }else{
                regex="[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领无]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))";
//                regex="[\u4e00-\u9fa5]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))";
            }
//            String regex="^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$";
            try{ 
                Pattern p = Pattern.compile(regex); //^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$
                Matcher m = p.matcher(vehicleID);
                boo = m.matches(); 
                
            }catch(Exception e){ 
                boo = false; 
            } 
        }
        return boo;
    }
    
    
    public static String subHeadandtail(String resourceStr,String replacechar){
        boolean sboo=true;//开始检查
        boolean eboo=true;//结束检查
        if(resourceStr==null||resourceStr.trim().isEmpty()){
            return "";
        }
        if(replacechar==null){
            return resourceStr;
        }
        int lenght=resourceStr.length();
        for(int i=0;i<lenght;i++){
            if(sboo){
                String sstr=resourceStr.substring(0,1);//开始值
                if(replacechar.contains(sstr)){//有
                    resourceStr=resourceStr.substring(1, resourceStr.length());
                    i++;
                }else{
                    sboo=false;
                }
            }
            if(eboo){
                String estr=resourceStr.substring(resourceStr.length()-1,resourceStr.length());//开始值
                if(replacechar.contains(estr)){//有
                    resourceStr=resourceStr.substring(0, resourceStr.length()-1);
                    i++;
                }else{
                    eboo=false;
                }
            }
            if(!sboo&&!eboo){
                break;
            }
        }
        return resourceStr;
    }
    
}