3

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.

Hamed Kamrava
  • 12,359
  • 34
  • 87
  • 125

4 Answers4

2

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));
          } 
    }

}
Identity1
  • 1,139
  • 16
  • 33
1

You can use more than one regular expression.* And... there are several solutions. One of them is to replace the redundant text by spaces. Then use these spaces to split the string. That is to say:

• Code:

String[] array = lines.replaceAll("(?m).*is\\s+|%\\s*", " ").trim().split("\\s+");
System.out.println(Arrays.toString(array));

• Output:

[0.00, 21.64, 3.29, 0.94, 3.76, 1.21, 86.09, 7.32]

────────────
* By the way, Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns is a good place to start to learn regular expressions. Mastering Regular Expressions, 3rd Edition is a highly recommended book.

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
0

You can also do this using split function if you want to -

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 lineArray[] = lines.split("\n");
        for(String temp:lineArray){
            String percentWithSign = temp.split("utilization is")[1];
            String percentStr  = percentWithSign.replace("%", "");
            Float percentFloat = Float.parseFloat(percentStr);
            System.out.println(percentFloat);
        }
Mandeep Rajpal
  • 1,667
  • 2
  • 11
  • 16
0

It can be solved without regular expression also at least in this case after seeing the pattern of your input.

List<String> list = new ArrayList<String>();
        list.add("36600.10: [Host #255] utilization is 0.00%");
        list.add("36600.10: [Host #256] utilization is 21.64%");
        list.add("36600.10: [Host #255] utilization is 3.29%");
        for(int i=0 ; i<list.size() ; i++){
            String str = list.get(i);
            int start = str.lastIndexOf(" ");
            String temp = str.substring(start+1, str.length()-1);
            System.out.println(temp);
        }
Narendra Jaggi
  • 1,297
  • 11
  • 33