800%cpu 79%user 7%nice 97%sys 538%idle 0%iow 45%irq 34%sirq 0%host
时间: 2024-02-14 19:12:02 浏览: 22
根据你提供的实际字符串,可以使用如下的代码来匹配所需的数据:
```java
String str = "800%cpu 79%user 7%nice 97%sys 538%idle 0%iow 45%irq 34%sirq 0%host";
Pattern pattern = Pattern.compile("(\\d+)%cpu\\s+(\\d+)%user\\s+(\\d+)%nice\\s+(\\d+)%sys\\s+(\\d+)%idle\\s+(\\d+)%iow\\s+(\\d+)%irq\\s+(\\d+)%sirq\\s+(\\d+)%host");
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
String cpu = matcher.group(1);
String user = matcher.group(2);
String nice = matcher.group(3);
String sys = matcher.group(4);
String idle = matcher.group(5);
String iow = matcher.group(6);
String irq = matcher.group(7);
String sirq = matcher.group(8);
String host = matcher.group(9);
System.out.println("CPU: " + cpu + "%");
System.out.println("User: " + user + "%");
System.out.println("Nice: " + nice + "%");
System.out.println("Sys: " + sys + "%");
System.out.println("Idle: " + idle + "%");
System.out.println("Iow: " + iow + "%");
System.out.println("IRQ: " + irq + "%");
System.out.println("SIRQ: " + sirq + "%");
System.out.println("Host: " + host + "%");
} else {
System.out.println("No match found");
}
```
正则表达式中的`\s+`表示匹配一个或多个空格字符。由于实际字符串中的空格数量可能不是固定的,因此使用`\s+`可以匹配不同数量的空格字符。
上述代码中,通过`matcher.group()`方法获取了匹配到的数据,并打印输出。如果没有匹配到任何数据,则输出“No match found”。
希望能够帮到你!