java正则匹配多个⼦字符串样例⽂本内容:
上海市黄浦区
瑞典
江苏省⽆锡市
⼴东省深圳市南⼭区
子字符串是什么我希望分别将字符串中的省份,城市名,城区名匹配出来,如匹配不出来就默认放在省份中。
1public static HashMap<String, String> splitCountry(String country) {
2        HashMap<String, String> ret = new HashMap<String, String>();
3        Pattern mpattern = Patternpile("(((.*省)|(.*市)|(.*区)).*?|.*)");
4        Matcher mmatcher = mpattern.matcher(country);
5        String str = "";
6while (mmatcher.find()) {
7            str = up();
8if (str.length() > 0) {
9if (dsWith("省"))
10                    ret.put("province", str);
11else if (dsWith("市"))
12                    ret.put("city", str);
13else if (dsWith("区"))
14                    ret.put("region", str);
15else
16                    ret.put("province", str);
17            }
18        }
19return ret;
20    }
1public class TestIP {
2
3    @Test
4public void test() {
5          List<String> cList = new ArrayList<String>();
6          cList.add("上海市黄浦区");
7          cList.add("瑞典");
8          cList.add("江苏省⽆锡市");
9                  cList.add("⼴东省深圳市南⼭区");
10for(String country:cList)
11          {
12              HashMap<String,String> dd = IPUtil.splitCountry(country);
13              System.out.("province") + "|" + dd.get("city") + "|" + dd.get("region"));
14          }
15    }
16
17 }
程序执⾏输出结果:
|上海市|黄浦区
瑞典|null|null
江苏省|⽆锡市|null
⼴东省|深圳市|南⼭区

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。