使用Regex匹配文本行

时间:2016-01-01 11:15:17

标签: java regex

我有这样的台词:

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%

我需要在utilization is之后获取所有数字。我想要的是这样的数组:

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

到目前为止我尝试了什么(它只适用于第一行):

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

    }

}

抱歉,我是java的新手并尝试了一些模式,但没有帮助。任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:2)

使用While循环。 If语句将限制匹配第一次出现(正在发生)

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

}

答案 1 :(得分:1)

您可以使用多个正则表达式。*并且......有几种解决方案。其中之一是用空格替换冗余文本。然后使用这些空格来分割字符串。也就是说:

•代码:

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

•输出:

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

<子>────────────
*顺便说一下,Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns是开始学习正则表达式的好地方。 Mastering Regular Expressions, 3rd Edition是一本值得推荐的书。

答案 2 :(得分:0)

如果您想 -

,也可以使用拆分功能执行此操作
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);
        }

答案 3 :(得分:0)

在看到输入模式后,至少在这种情况下也可以在没有正则表达式的情况下解决。

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