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(" {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 if(vehicleID.length()==8){ // regex = "^([京津沪æ¸å†€è±«äº‘è¾½é»‘æ¹˜çš–é²æ–°è‹æµ™èµ£é„‚桂甘晋蒙陕å‰é—½è´µç²¤é’è—å·å®ç¼ä½¿é¢†æ— ][a-zA-Z](([ADF](?![IO])[A-Z0-9][0-9]{4})|([0-9]{5}[DF])))$"; //regex = "^([京津沪æ¸å†€è±«äº‘è¾½é»‘æ¹˜çš–é²æ–°è‹æµ™èµ£é„‚桂甘晋蒙陕å‰é—½è´µç²¤é’è—å·å®ç¼ä½¿é¢†æ— ][a-zA-Z](([A-Z](?![IO])[A-Z0-9][0-9]{4})|([0-9]{5}[DF])))$"; regex = "^[京津沪æ¸å†€è±«äº‘è¾½é»‘æ¹˜çš–é²æ–°è‹æµ™èµ£é„‚桂甘晋蒙陕å‰é—½è´µç²¤é’è—å·å®ç¼ä½¿é¢†æ— A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([A-HJ-K][A-HJ-NP-Z0-9][0-9]{4}$))"; } 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; } }