1. 程式人生 > >天貓,淘寶,京東收貨信息中,自動識別手機號、姓名、省市區

天貓,淘寶,京東收貨信息中,自動識別手機號、姓名、省市區

reader 經緯度 nod val tostring pri location com matcher

最近工作需要,需要從一串字符串中識別出姓名,手機號和省市區,例如下面的一些字符串:

櫥之友,包衛貞,13600000000,浙江省寧波市,慈溪市,庵東鎮 杭州灣新區世紀城翠湖苑5棟2000
【識別出】姓名為“包衛貞”,手機號為“13600000000”,省市區為“浙江省 寧波市 慈溪市”
marui謝sir,謝義海,13900000000,江蘇省南京市,秦淮區,健康路1號水遊城5樓1號
【識別出】姓名為“謝義海”,手機號為“13900000000”,省市區為“江蘇省 南京市 秦淮區”
劉偉,13120000000,北京 北京市 朝陽區 東湖街道 利澤中園二區208號,000000
【識別出】姓名為“劉偉”,手機號為“13120000000”,省市區為“北京 北京市 朝陽區”

在網上找了很多資料,都沒有解決,下面是我的詳細解決思路和Java代碼。

【解決思路】

1.最先識別出11位手機號,這個最簡單

2.其次識別出姓名,姓名難度有點大,也沒找到好的方法,我是從姓氏下手識別的。

3.最後識別出省市區,這個難度也很大,試了很多方法,最好找到一種方法,效果不錯。

如果需要源碼,可以去下載。

【java代碼】

1.識別手機號

?private static String getMobile(String addressInfo) {
?? ? ? ? ? ?//識別電話號碼/
?? ? ? ? ? ?Pattern pattern = Pattern.compile("([1][3-9][\d]{9})|(0\d{2,4}-\d{7,8})");

?? ? ? ? ? ?Matcher matcher = pattern.matcher(addressInfo);
?? ? ? ? ? ?StringBuffer bf = new StringBuffer(64);
?? ? ? ? ? ?while (matcher.find()) {
?? ? ? ? ? ? ? ?bf.append(matcher.group());
?? ? ? ? ? ?}
?? ? ? ? ? ?return bf.toString();
?? ? ? ?}

2.識別姓名

?? ? ? ?private static String getName(String addressInfo,String splitStr) {

?? ? ? ? ? ?//識別姓名/
?? ? ? ??? ?String[] firstNames = {"趙","錢","孫","李","周","吳","鄭","王","馮","陳","褚","衛","蔣","沈","韓","楊","朱","秦","尤","許","何","呂","施","張","孔","曹","嚴","華","金","魏","陶","姜","戚","謝","鄒","喻","柏","水","竇","章","雲","蘇","潘","葛","奚","範","彭","郎","魯","韋","昌","馬","苗","鳳","花","方","俞","任","袁","柳","酆","鮑","史","唐","費","廉","岑","薛","雷","賀","倪","湯","滕","殷","羅","畢","郝","鄔","安","常","樂","於","時","傅","皮","卞","齊","康","伍","余","元","蔔","顧","孟","平","黃","和","穆","蕭","尹","姚","邵","湛","汪","祁","毛","禹","狄","米","貝","明","臧","計","伏","成","戴","談","宋","茅","龐","熊","紀","舒","屈","項","祝","董","粱","杜","阮","藍","閔","席","季","麻","強","賈","路","婁","危","江","童","顏","郭","梅","盛","林","刁","鐘","徐","邱","駱","高","夏","蔡","田","樊","胡","淩","霍","虞","萬","支","柯","昝","管","盧","莫","經","房","裘","繆","幹","解","應","宗","丁","宣","賁","鄧","郁","單","杭","洪","包","諸","左","石","崔","吉","鈕","龔","程","嵇","邢","滑","裴","陸","榮","翁","荀","羊","於","惠","甄","麯","家","封","芮","羿","儲","靳","汲","邴","糜","松","井","段","富","巫","烏","焦","巴","弓","牧","隗","山","谷","車","侯","宓","蓬","全","郗","班","仰","秋","仲","伊","宮","寧","仇","欒","暴","甘","鈄","厲","戎","祖","武","符","劉","景","詹","束","龍","葉","幸","司","韶","郜","黎","薊","薄","印","宿","白","懷","蒲","邰","從","鄂","索","鹹","籍","賴","卓","藺","屠","蒙","池","喬","陰","欎","胥","能","蒼","雙","聞","莘","黨","翟","譚","貢","勞","逄","姬","申","扶","堵","冉","宰","酈","雍","舄","璩","桑","桂","濮","牛","壽","通","邊","扈","燕","冀","郟","浦","尚","農","溫","別","莊","晏","柴","瞿","閻","充","慕","連","茹","習","宦","艾","魚","容","向","古","易","慎","戈","廖","庾","終","暨","居","衡","步","都","耿","滿","弘","匡","國","文","寇","廣","祿","闕","東","毆","殳","沃","利","蔚","越","夔","隆","師","鞏","厙","聶","晁","勾","敖","融","冷","訾","辛","闞","那","簡","饒","空","曾","毋","沙","乜","養","鞠","須","豐","巢","關","蒯","相","查","後","荊","紅","遊","竺","權","逯","蓋","益","桓","公","萬俟","司馬","上官","歐陽","夏侯","諸葛","聞人","東方","赫連","皇甫","尉遲","公羊","淡臺","公冶","宗政","濮陽","淳於","單於","太叔","申屠","公孫","仲孫","軒轅","令狐","鐘離","宇文","長孫","慕容","鮮於","閭丘","司徒","司空","亓官","司寇","仉","督","子車","顓孫","端木","巫馬","公西","漆雕","樂正","壤駟","公良","拓跋","夾谷","宰父","谷梁","晉","楚","閆","法","汝","鄢","塗","欽","段幹","百裏","東郭","南門","呼延","歸","海","羊舌","微生","嶽","帥","緱","亢","況","後","有","琴","梁丘","左丘","東門","西門","商","牟","佘","佴","伯","賞","南宮","墨","哈","譙","笪","年","愛","陽","佟"};
?? ? ? ? ? ?String[] names=addressInfo.split(splitStr);
?? ? ? ? ? ?String name="";
?? ? ? ? ? ?for(int i=0;i<names.length;i++){
?? ? ? ? ? ??? ?String nametmp=names[i];
?? ? ? ? ? ??? ?//System.out.println(names.length+"//"+nametmp);
?? ? ? ? ? ??? ?if(nametmp==null||nametmp.length()<=1||nametmp.length()>4){
?? ? ? ? ? ??? ??? ?continue;
?? ? ? ? ? ??? ?}
?? ? ? ? ? ??? ?for(int j=0;j<firstNames.length;j++){
?? ? ? ? ? ??? ??? ?if(nametmp.startsWith(firstNames[j])){
?? ? ? ? ? ??? ??? ??? ?name=nametmp;
?? ? ? ? ? ??? ??? ??? ?break;
?? ? ? ? ? ??? ??? ?}
?? ? ? ? ? ??? ?}
?? ? ? ? ? ??? ?if(!"".equals(name)){
?? ? ? ? ? ??? ??? ?break;
?? ? ? ? ? ??? ?}
?? ? ? ? ? ?}
?? ? ? ? ? ?return name;
?? ? ? ?}

            3.識別省市區

?public static Map<String,String> getAddress(String address) throws MalformedURLException{
?? ??? ?//根據市和詳細地址獲取地址經緯度
?? ??? ?String LatitudeAndLongitude = getGeocoderLatitude(address, "");
?? ??? ?JsonNode jsonNode = getposition(LatitudeAndLongitude);
?? ??? ?String province = jsonNode.findValue("province").toString().replace("\"","");
?? ??? ?String city = jsonNode.findValue("city").toString().replace("\"","");
?? ??? ?String area = jsonNode.findValue("district").toString().replace("\"","");
?? ??? ?String street=jsonNode.findValue("street").toString().replace("\"","");
?? ??? ?Map<String,String> addMap = new HashMap<String,String>();
?? ??? ?addMap.put("province", province);
?? ??? ?addMap.put("city", city);
?? ??? ?addMap.put("area", area);
?? ??? ?addMap.put("street", street);
?? ??? ?return addMap;?? ??? ?
?? ?}

?? ?/
?? ? 根據經緯度查詢地址信息
?? ?
@param
?? ? @throws MalformedURLException
?? ?
/
?? ?public static JsonNode getposition(String LatitudeAndLongitude)
?? ??? ??? ?throws MalformedURLException {
?? ??? ?BufferedReader in = null;
?? ??? ?JsonNode locationNode = null;
?? ??? ?/URL tirc = new URL("http://api.map.baidu.com/geocoder?location="
?? ??? ??? ??? ?+ LatitudeAndLongitude + "&output=json&key="
?? ??? ??? ??? ?+ "
");/
?? ??? ?URL tirc = new URL("http://api.map.baidu.com/geocoder/v2/?location="
?? ??? ??? ??? ?+ LatitudeAndLongitude + "&output=json&ak="
?? ??? ??? ??? ?+ ak);
?? ??? ?try {
?? ??? ??? ?in = new BufferedReader(new InputStreamReader(tirc.openStream(),
?? ??? ??? ??? ??? ?"UTF-8"));
?? ??? ??? ?String res;
?? ??? ??? ?StringBuilder sb = new StringBuilder("");
?? ??? ??? ?while ((res = in.readLine()) != null) {
?? ??? ??? ??? ?sb.append(res.trim());
?? ??? ??? ?}
?? ??? ??? ?String str = sb.toString();
?? ??? ??? ?ObjectMapper mapper = new ObjectMapper();
?? ??? ??? ?if (StringUtils.isNotEmpty(str)) {
?? ??? ??? ??? ?JsonNode jsonNode = mapper.readTree(str);
?? ??? ??? ??? ?jsonNode.findValue("status").toString();
?? ??? ??? ??? ?JsonNode resultNode = jsonNode.findValue("result");
?? ??? ??? ??? ?/
JsonNode locationNode = resultNode
?? ??? ??? ??? ??? ??? ?.findValue("formatted_address");/
?? ??? ??? ??? ?locationNode = resultNode
?? ??? ??? ??? ??? ??? ?.findValue("addressComponent");
?? ??? ??? ??? ?/
JsonNode area = locationNode.findValue("district");
?? ??? ??? ??? ?System.out.println(area);*/
?? ??? ??? ?}

?? ??? ?} catch (Exception e) {

?? ??? ?}
?? ??? ?return locationNode;

?? ?}

整個MyEclipse項目源碼下載,導入到MyEclipse,運行測試代碼就可看到效果。

天貓,淘寶,京東收貨信息中,自動識別手機號、姓名、省市區