Jes Jes - 3 years ago 68
Java Question

how to extract a substring from a given string in java

I want to extract substring of a given string.
the example string is

rta=0.037ms;3000.000;5000.000;0; pl=10%;80;100;; rtmax=0.125ms;;;; rtmin=0.012ms;


I want to get only 0.037ms after "rta=" and percent after pl=. I tried to splite the above string by space and then by semicolon. did not work.

Answer Source
String s = "rta=0.037ms;3000.000;5000.000;0; pl=10%;80;100;; rtmax=0.125ms;;;; rtmin=0.012ms;";
Pattern pattern = Pattern.compile("rta=(.*?);.*pl=(.*?);");
Matcher matcher = pattern.matcher(s);
if(matcher.find()){
    System.out.println(matcher.group(1));
    System.out.println(matcher.group(2));
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download