日期:2014-05-20  浏览次数:20631 次

请问如何用Java的正规表达式提取我想要的信息?
字符串格式如下,但每列的长度不一定一致,用“,”或者“,空格”隔开。

V,20120202,235900,59651,P,00998, 0, 4, 6.3408, 6.1846, 0.1562,19675, 9.08124,Km, 0.142276,PPP
V,20120202,240000,59651,P,00998, 0, 4, 6.3438, 6.193, 0.1505,19675, 20.97290,Km, 0.143011,PPP


请问如何分别提取20120202 235900 和 9.09124

------解决方案--------------------
Java code


        String str = "V,20120202,235900,59651,P,00998, 0, 4, 6.3408, 6.1846, 0.1562,19675, 9.08124,Km, 0.142276,PPP";
        
        Pattern p = Pattern.compile("[^,]+,\\s*([^,]+),\\s*([^,]+)(,[^,]+){9},\\s*([^,]+).*");
        Matcher m = p.matcher(str);
        if(m.matches())
        {
            System.out.println(m.group(1));
            System.out.println(m.group(2));
            System.out.println(m.group(4));
        }