解析纯文本

时间:2013-07-29 07:15:21

标签: java parsing plaintext

我如何解析这个字符串:

  

PING 192.168.1.2(192.168.1.2)56(84)字节数据.64字节来自192.168.1.2:icmp_seq = 1 ttl = 64时间= 0.244 ms64字节来自192.168.1.2:icmp_seq = 2 ttl = 64时间= 0.274 ms64字节来自192.168.1.2:icmp_seq = 3 ttl = 64 time = 0.275 ms64字节来自192.168.1.2:icmp_seq = 4 ttl = 64 time = 0.306 ms64字节来自192.168.1.2:icmp_seq = 5 ttl = 64 time = 0.550 ms --- 192.168.1.2 ping统计---发送5个数据包,5个接收,0%丢包,时间4001msrtt min / avg / max / mdev = 0.244 / 0.329 / 0.550 / 0.114 ms

我尝试使用StringTokenizer但未获得适当的结果。

我试过的代码如下:

StringTokenizer tokens = new StringTokenizer(pingResult, ":");
String first = tokens.nextToken();// this will contain "Fruit"
String second = tokens.nextToken();

4 个答案:

答案 0 :(得分:3)

尝试正则表达式匹配:

public static void findMatch(){
        String str = "PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.64 bytes from 192.168.1.2" +
                ": icmp_seq=1 ttl=64 time=0.244 ms64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=0.274 ms64 " +
                "bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=0.275 ms64 bytes from 192.168.1.2: icmp_seq=4 ttl=64 time=0.306 ms64 " +
                "bytes from 192.168.1.2: icmp_seq=5 ttl=64 time=0.550 ms--- 192.168.1.2 ping statistics ---5 packets transmitted, " +
                "5 received, 0% packet loss, time 4001msrtt min/avg/max/mdev = 0.244/0.329/0.550/0.114 ms";

        String regexPattern = "icmp_seq=\\d+ ttl=\\d+ time=.+?ms";
        Pattern pattern = Pattern.compile(regexPattern);

        Matcher matcher = pattern.matcher(str);

        while (matcher.find()){
            System.out.println(str.substring(matcher.start(), matcher.end()));
        }
    }

结果:

icmp_seq = 1 ttl = 64 time = 0.244 ms

icmp_seq = 2 ttl = 64 time = 0.274 ms

icmp_seq = 3 ttl = 64 time = 0.275 ms

icmp_seq = 4 ttl = 64 time = 0.306 ms

icmp_seq = 5 ttl = 64 time = 0.550 ms

答案 1 :(得分:2)

试试这个,

 while(tokens.hasMoreElements())
{
   String subStringValue = tokens.nextToken();
   System.out.println("token : " + StringUtils.substringBetween(subStringValue, "", "ms")+"ms");
}

StringUtils.substringBetween

答案 2 :(得分:2)

单独StringTokenizer无法工作。 StringTokenizer substring可以用StringTokenizer来达到预期的效果。即icmp_seq=1 ttl=64 time=0.244 ms64 bytes from 192.168.1.2会将令牌作为" String finalString=tokenizedString.substring(0,tokenizedString.indexOf(ms)+1); "。您可以从标记化的String中执行子字符串。类似的东西:

{{1}}

虽然答案有点脏,但它解决了目的。

答案 3 :(得分:1)

您可以使用String split方法。

ArrayList<String> ResultTab= new ArrayList<String>();
    String pingResult = "PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.64 bytes from 192.168.1.2: icmp_seq=1 ttl=64 time=0.244 ms64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=0.274 ms64 bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=0.275 ms64 bytes from 192.168.1.2: icmp_seq=4 ttl=64 time=0.306 ms64 bytes from 192.168.1.2: icmp_seq=5 ttl=64 time=0.550 ms--- 192.168.1.2 ping statistics ---5 packets transmitted, 5 received, 0% packet loss, time 4001msrtt min/avg/max/mdev = 0.244/0.329/0.550/0.114 ms";
    String[] pingResultTab = pingResult.split(":");
    for (int i = 0; i < pingResultTab.length; i++) {
        System.out.println(pingResultTab[i]);
        if(i >= 1)
        {
            String[] itTab = pingResultTab[i].split("ms");
            ResultTab.add(itTab[0]);


        }

    }
    System.out.println("---RESULTAT-------");
    for (String result : ResultTab) {
        System.out.println(result);

    }