请问怎么用java正则表达式提取以下文本中指定的内容?
20150819-00:00:01avg-cpu:%user%nice%system%iowait%steal%idle38.710.003.603.150.0054.5...
20150819-00:00:01
avg-cpu: %user %nice %system %iowait %steal %idle
38.71 0.00 3.60 3.15 0.00 54.54
Device: wsec/s avgrq-sz avgqu-sz await svctm %util
sda 4110.60 68.36 0.38 5.40 4.62 32.65
sda1 0.00 18.83 0.00 10.20 8.92 0.00
sda2 3866.36 77.01 0.01 7.02 5.16 29.60
#Hard Utilization#
/ is already use: 72%
/boot is already use: 1%
/home is already use: 63%
#Memery Utilization#
system memery is already use: 75.18%
请问怎么从以上文本中读取%user想对应的数据,%util相对应的数据,use相对应的数据?其中%util和use的数据的规律是可以使用正则表达式提取最后一个结尾,但是%user的数据不知道怎么提取? 展开
avg-cpu: %user %nice %system %iowait %steal %idle
38.71 0.00 3.60 3.15 0.00 54.54
Device: wsec/s avgrq-sz avgqu-sz await svctm %util
sda 4110.60 68.36 0.38 5.40 4.62 32.65
sda1 0.00 18.83 0.00 10.20 8.92 0.00
sda2 3866.36 77.01 0.01 7.02 5.16 29.60
#Hard Utilization#
/ is already use: 72%
/boot is already use: 1%
/home is already use: 63%
#Memery Utilization#
system memery is already use: 75.18%
请问怎么从以上文本中读取%user想对应的数据,%util相对应的数据,use相对应的数据?其中%util和use的数据的规律是可以使用正则表达式提取最后一个结尾,但是%user的数据不知道怎么提取? 展开
推荐于2017-12-16
展开全部
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | // 回答完毕,采纳即可 import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Drinks { public static final String LINE = System.getProperty( "line.separator" ); public static void main(String[] args) { try { String result = "" ; Scanner scanner = new Scanner( new File( "data.txt" )); while (scanner.hasNextLine()) { String line = scanner.nextLine(); result += line + LINE; } String userRegex = "[\\s\\S]*%user(\\s+%\\w+){5}[\r\n\\s]+([\\d\\.]+)\\s[\\s\\S]*" ; System.out.println( "%user想对应的数据: " + result.replaceAll(userRegex, "$2" )); String utilRegex = "sda.*\\s([\\d\\.]+)" ; Pattern pattern = Pattern.compile(utilRegex); Matcher matcher = pattern.matcher(result); while (matcher.find()) { System.out.println( "%util相对应的数据: " + matcher.group( 1 )); } String useRegex = "use:\\s*([\\d\\.%]+)" ; pattern = Pattern.compile(useRegex); matcher = pattern.matcher(result); while (matcher.find()) { System.out.println( "use相对应的数据: " + matcher.group( 1 )); } } catch (FileNotFoundException e) { e.printStackTrace(); } } } |
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询