Administrator
2022-09-14 58d006e05dcf2a20d0ec5367dd03d66a61db6849
提交 | 用户 | 时间
58d006 1
A 2 package com.mandi.common.ParamFileter;
3
4 import java.lang.reflect.Field;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern;
9
10 import com.mandi.dao.common.ObjectResult;
11
12
13
14 /** 
15  * 正则表达式验证 <br/> 
16  * 2016年4月10日 下午4:37:21 <br/> 
17  * @author   guolq        
18  */
19 public class RegexCheck {
20     
21     
22     /** 
23      * checkphone:验证手机号码11位
24      * @param phone
25      * @return 
26      * TODO
27      */  
28     public static boolean checkphone(String phone){
29         boolean flag = false; 
30         if(phone==null||phone.trim().isEmpty())
31             return flag;
32         try{ 
33             Pattern p = Pattern.compile("^[1][0-9]{10}$"); //^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$
34             Matcher m = p.matcher(phone);
35             flag = m.matches(); 
36         }catch(Exception e){ 
37             flag = false; 
38         } 
39             return flag;
40     }
41     
42     /** 
43      * checkempty:字符串不能为空和null
44      * @param strargs
45      * @return 
46      * TODO
47      */  
48     public static boolean checkempty(String...strargs){
49         for (String str : strargs) {
50             if (str==null||str.trim().isEmpty()) {
51                 return false;
52             }
53         }
54         return true;
55     }
56     public static <T> String checkempty(T obj,String[][] fieldandname){
57         StringBuffer bf=new StringBuffer();
58         if(obj==null)
59         {
60             bf.append("对象不存在!");
61             return bf.toString();
62         }
63         @SuppressWarnings("rawtypes")
64         Class cls=obj.getClass();
65         try {
66             if(fieldandname==null||fieldandname[0].length<=0)
67                 return bf.toString();
68             for(int i=0;i<fieldandname[0].length;i++)
69             {
70                 String fields=fieldandname[0][i];
71                 Field prop=cls.getDeclaredField(fields);
72                 prop.setAccessible(true);
73                 if(prop.get(obj)==null||prop.get(obj).toString().trim().isEmpty())
74                 {
75                     if(bf.toString().length()!=0)
76                         bf.append(","+fieldandname[1][i]);
77                     else
78                         bf.append(fieldandname[1][i]);
79                 }
80             }
81             if(bf.length()!=0)
82                 bf.append("  为必填项不能为空!");
83         } catch (Exception e) {
84             e.printStackTrace();
85             bf.setLength(0);
86             bf.append("必填项不能为空获取出错!");
87             return bf.toString();
88         }
89         return bf.toString();
90     }
91     
92     /** 
93      * 复制相同对象
94      * @param reobj 返回对象
95      * @param orobj 源对象
96      * @param fiedsName 字段名数组
97      * @return 
98      */  
99     public static <T> T copyobj(T reobj,T orobj,String[] fiedsName){
100         if(reobj==null)
101             return null;
102         if(orobj==null||fiedsName==null||fiedsName.length<=0)
103             return reobj;
104         @SuppressWarnings("rawtypes")
105         Class recls=reobj.getClass();
106         @SuppressWarnings("rawtypes")
107         Class orcls=orobj.getClass();
108         for(int i=0;i<fiedsName.length;i++)
109         {
110             String fname=fiedsName[i];
111             try {
112                 Field orprop = orcls.getDeclaredField(fname);
113                 Field reprop=recls.getDeclaredField(fname);
114                 if(orprop==null||reprop==null)
115                     continue;
116                 orprop.setAccessible(true);
117                 reprop.setAccessible(true);
118                 reprop.set(reobj, orprop.get(orobj));
119             } catch (Exception e) {
120                 e.printStackTrace();
121             } 
122         }
123         return reobj;
124     }
125     public static <T> Map<String, Object> ObjtoMap(T Obj,String[] fiedsName){
126         Map<String, Object> mp=new HashMap<String, Object>();
127         if(Obj==null)
128             return mp;
129         @SuppressWarnings("rawtypes")
130         Class recls=Obj.getClass();
131         if(fiedsName==null||fiedsName.length<=0){
132             Field[] ff=recls.getFields();
133             for (int i = 0; i < ff.length; i++) {
134                 
135 //                mp.put(, );
136             }
137         }else
138         {
139             for(int i=0;i<fiedsName.length;i++)
140             {
141                 String fname=fiedsName[i];
142                 try {
143 //                    Field orprop = orcls.getDeclaredField(fname);
144                     Field reprop=recls.getDeclaredField(fname);
145 //                    if(orprop==null||reprop==null)
146 //                        continue;
147 //                    orprop.setAccessible(true);
148                     reprop.setAccessible(true);
149 //                    reprop.set(Obj, orprop.get(orobj));
150                 } catch (Exception e) {
151                     e.printStackTrace();
152                 } 
153             }
154         }
155         
156         return null;
157     }
158     
159     
160     /** 
161      * checkpassword:验证密码某些格式
162      * @param pwd
163      * @param oldpwd
164      * @return 
165      * TODO
166      */  
167     public static ObjectResult<Boolean> checkpassword(String pwd,String oldpwd){
168         ObjectResult<Boolean> objresult =new ObjectResult<Boolean>();
169         try {
170             if(oldpwd==null||pwd==null||oldpwd.trim().isEmpty()||pwd.trim().isEmpty()){
171                 objresult.setCode(1);
172                 objresult.setHint("密码不能设置为空");
173                 objresult.setItem(false);
174                 return objresult;
175             }
176             //pwd=pwd.replaceAll("\\s*|\t|\r|\n| |    |", "").replaceAll(" | ", "");//去除字符串中所包含的空格(包括:空格(半角)、制表符、换页符等) 
177             Pattern p = Pattern.compile("(?!^\\d+$)(?!^[a-zA-Z]+$).{6,16}"); 
178             Matcher m = p.matcher(pwd);
179             boolean boo = m.matches(); 
180             if(boo){
181                 objresult.setCode(0);
182                 objresult.setHint(pwd);
183                 objresult.setItem(boo);
184             }else{
185                 objresult.setCode(1);
186                 objresult.setHint("密码格式必须包含:字母和数字(可以包含特殊字符)的6到16位的字符串");
187                 objresult.setItem(boo);
188             }
189         } catch (Exception e) {
190             objresult.setCode(1);
191             objresult.setHint("密码检验出错,请重新输入密码!!");
192         }
193         return objresult;    
194     }
195     
196     
197     /** 
198      * getsearchwords:简单处理分割一些搜索词
199      * @param shword
200      * @return 
201      * TODO
202      */  
203     public static String getsearchwords(String shword){
204         
205         if (shword==null||shword.trim().isEmpty()) 
206             return null;
207         if (shword.length()>200) {
208             shword=shword.substring(0, 200);//200个字符
209         }
210         
211         shword=shword.replaceAll(" |,|,|,|    |    ", " ");//替换一些特殊字符
212         shword=shword.replaceAll("&nbsp{2,}", " ");
213         String[] strs=shword.trim().split(" ");
214         if (strs==null) 
215             return null;
216         StringBuffer strbuf=new StringBuffer();
217         for (int i =0; i<strs.length; i++) {
218             if (strs[i].trim().isEmpty()) 
219                 continue;
220             strbuf.append(strs[i]+",");
221         }
222         return strbuf.toString();
223         
224     }
225     
226     public static boolean isIP(String ipaddress){
227             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]))";   
228            Pattern pattern = Pattern.compile(ip);   
229            Matcher matcher = pattern.matcher(ipaddress);   
230            return matcher.matches();   
231     }
232     
233     /** 
234      * getRadm:生成任意位数的随机字符串
235      * @param panflag
236      * @param randomnum
237      * @return 
238      * TODO
239      */  
240     public static String  getRadm(int panflag,int randomnum){
241         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'};
242         StringBuffer result =new StringBuffer();
243         if(panflag==1){
244             for (int i = 0; i < randomnum ; i++) {
245                 int indx=(int)(Math.random()*340);
246                 indx=indx%34;
247                 result.append(chars[indx]);
248             }
249         }else if(panflag==2){
250             for (int i = 0; i < randomnum ; i++) {
251                 int indx=(int)(Math.random()*580);
252                 indx=indx%58;
253                 result.append(chars[indx]);
254             }
255         }else if(panflag==3){
256             for (int i = 0; i < randomnum ; i++) {
257                 int indx=(int)(Math.random()*100);
258                 indx=indx%10;
259                 result.append(chars[indx]);
260             }
261         }
262         return result.toString();
263     }
264     
265     /** 
266      * repllusername:字符串替换姓名 匿名
267      * @param username
268      * @return 
269      * TODO
270      */  
271     public static String repllusername(String username){
272         if (username==null||username.trim().isEmpty()) {
273             return "y***"+9;
274         }
275         if (username.length()<=1) {
276             return username+"***"+9;
277         }
278         if (username.length()==2) {
279             return username.substring(0,1)+"***"+username.substring(1);
280         }
281         return username.substring(0,1)+"***"+username.substring(username.length()-1);
282     }
283     
284     public static boolean isVehicleID(String vehicleID)
285     {
286         boolean boo=false;
287         if(vehicleID!=null&&!vehicleID.trim().isEmpty())
288         {
289             //拖拉机
290             String regex="";
291             if(vehicleID.length()==7){
292                 regex="[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领无]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$";
293 //                regex="[\u4e00-\u9fa5]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$";
294             }else{
295                 regex="[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领无]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))";
296 //                regex="[\u4e00-\u9fa5]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))";
297             }
298 //            String regex="^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$";
299             try{ 
300                 Pattern p = Pattern.compile(regex); //^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$
301                 Matcher m = p.matcher(vehicleID);
302                 boo = m.matches(); 
303                 
304             }catch(Exception e){ 
305                 boo = false; 
306             } 
307         }
308         return boo;
309     }
310     
311     
312     public static String subHeadandtail(String resourceStr,String replacechar){
313         boolean sboo=true;//开始检查
314         boolean eboo=true;//结束检查
315         if(resourceStr==null||resourceStr.trim().isEmpty()){
316             return "";
317         }
318         if(replacechar==null){
319             return resourceStr;
320         }
321         int lenght=resourceStr.length();
322         for(int i=0;i<lenght;i++){
323             if(sboo){
324                 String sstr=resourceStr.substring(0,1);//开始值
325                 if(replacechar.contains(sstr)){//有
326                     resourceStr=resourceStr.substring(1, resourceStr.length());
327                     i++;
328                 }else{
329                     sboo=false;
330                 }
331             }
332             if(eboo){
333                 String estr=resourceStr.substring(resourceStr.length()-1,resourceStr.length());//开始值
334                 if(replacechar.contains(estr)){//有
335                     resourceStr=resourceStr.substring(0, resourceStr.length()-1);
336                     i++;
337                 }else{
338                     eboo=false;
339                 }
340             }
341             if(!sboo&&!eboo){
342                 break;
343             }
344         }
345         return resourceStr;
346     }
347     
348 }