I have lines like this :
36600.10: [Host #255] utilization is 0.00%
36600.10: [Host #256] utilization is 21.64%
36600.10: [Host #257] utilization is 3.29%
36600.10: [Host #258] utilization is 0.94%
36600.10: [Host #260] utilization is 3.76%
36600.10: [Host #260] utilization is 1.21%
36600.10: [Host #260] utilization is 86.09%
36600.10: [Host #260] utilization is 7.32%
I need to get all numbers after utilization is. What I want is an array like this :
myArray[0] => 0.00,
myArray[1] => 21.64,
myArray[2] => 3.29,
myArray[3] => 0.94,
myArray[4] => 3.76,
myArray[5] => 1.21,
myArray[6] => 7.32
What I tried so far (it just works for first line) :
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class main {
public static void main(String[] args) {
String lines = "36600.10: [Host #256] utilization is 21.65% \n 36600.10: [Host #256] utilization is 91.78% \n 36600.10: [Host #256] utilization is 3.29%";
String pattern = "(utilization is\\s)(\\d+\\.\\d*)(.*)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(lines);
if (m.find( )) {
System.out.println(m.group(2));
} else {
System.out.println("NO MATCH");
}
}
}
Sorry I'm new in java and tried some patterns but not helped. Any helps would be appreciated.
Use While loop. If statement will limit the match with the first occurence (Which is happening)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class main {
public static void main(String[] args) {
String lines = "36600.10: [Host #256] utilization is 21.65% \n 36600.10: [Host #256] utilization is 91.78% \n 36600.10: [Host #256] utilization is 3.29%";
String pattern = "(utilization is\\s)(\\d+\\.\\d*)(.*)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(lines);
while (m.find()) {
System.out.println(m.group(2));
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With