[关闭]
@liruiyi962464 2025-07-17T10:23:20.000000Z 字数 4899 阅读 69

String格式化正则表达式

代码


  1. public static void main(String[] args) throws IOException {
  2. String text = "总用量:0.490m³,阀门状态:On,电压:3.7V,当前单价:0.00元/m³,余额:0.00元,结算周期用量:0.000m³/0.00元,rssi:-129,";
  3. // 正则表达式模式,用于匹配阀门状态、余额和rssi字段
  4. String pattern = "阀门状态:(.*?),.*?余额:(.*?)元,.*?rssi:(.*?),$";
  5. // 创建 Pattern 对象
  6. Pattern r = Pattern.compile(pattern);
  7. // 创建 Matcher 对象
  8. Matcher m = r.matcher(text);
  9. if (m.find()) {
  10. String valveStatus = m.group(1); // 第一个括号里的内容是阀门状态
  11. String balance = m.group(2); // 第二个括号里的内容是余额
  12. String rssi = m.group(3); // 第三个括号里的内容是rssi
  13. System.out.println("阀门状态: " + valveStatus);
  14. System.out.println("余额: " + balance);
  15. System.out.println("rssi: " + rssi);
  16. } else {
  17. System.out.println("未找到匹配的内容");
  18. }
  19. }
  1. public static void main(String[] args) {
  2. // 示例数据
  3. List<String> resList = Arrays.asList(
  4. "PING 10.51.1.85 (10.51.1.85): 56 data bytes",
  5. "64 bytes from 10.51.1.85: seq=0 ttl=123 time=165.331 ms",
  6. "64 bytes from 10.51.1.85: seq=1 ttl=123 time=22.958 ms",
  7. "64 bytes from 10.51.1.85: seq=2 ttl=123 time=22.418 ms",
  8. "64 bytes from 10.51.1.85: seq=3 ttl=123 time=38.859 ms",
  9. "64 bytes from 10.51.1.85: seq=4 ttl=123 time=46.738 ms",
  10. "",
  11. "--- 10.51.1.85 ping statistics ---",
  12. "5 packets transmitted, 5 packets received, 0% packet loss",
  13. "round-trip min/avg/max = 22.418/59.260/165.331 ms",
  14. ""
  15. );
  16. String s = "";
  17. //获取最短时延,平均时延,最大时延
  18. if (resList != null || resList.size() > 0) {
  19. String line = resList.get(resList.size() - 2);
  20. System.out.println(line);
  21. String pattern = "min/avg/max = (\\d+\\.\\d+)/(\\d+\\.\\d+)/(\\d+\\.\\d+) ms";
  22. Pattern r = Pattern.compile(pattern);
  23. Matcher m = r.matcher(line);
  24. if (m.find()) {
  25. String min = calc(m.group(1));
  26. String avg = calc(m.group(2));
  27. String max = calc(m.group(3));
  28. s = "ping次数为" + 5 + ",最小时延为" + min + "ms,最大时延为" + max + "ms,平均时延为" + avg + "ms";
  29. System.err.println(s);
  30. }else {
  31. System.err.println("ping结果为空");
  32. }
  33. }
  34. }
  1. String line = resList.get(resList.size() - 3);
  2. String pattern = "min/avg/max = (\\d+\\.\\d+)/(\\d+\\.\\d+)/(\\d+\\.\\d+) ms";
  3. Pattern r = Pattern.compile(pattern);
  4. Matcher m = r.matcher(line);
  5. if (m.find()) {
  6. String min = calc(m.group(1));
  7. String avg = calc(m.group(2));
  8. String max = calc(m.group(3));
  9. s = "ping次数为" + validationParameter.getPingCount() + ",最小时延为" + min + "ms,最大时延为" + max + "ms,平均时延为" + avg + "ms";
  10. }
  1. private String attachResultHandle(Set<SysUser> userList,DeviceInfo deviceInfo,Object result, BusinessValidationParameter validationParameter, BusinessValidationDetails businessValidationDetails) {
  2. //处理结果
  3. String s = ""; //attach时延结果
  4. Double successCount = 0.00; //attach时延不为error的次数
  5. int m = 0; // attach次数
  6. int totalDelay = 0; //attach时延总和
  7. int minDelay = 0; //attach时延最小值
  8. int maxDelay = 0; //attach时延最大值
  9. int countDelayed = 0; //attach时延不为error的次数
  10. int errorCount = 0; // attach时延为error的次数
  11. String strResult = result.toString(); //attach时延结果
  12. strResult = strResult.substring(1, strResult.length() - 2); //attach时延结果
  13. List<String> resList = Arrays.asList(strResult.split(",")); //attach时延结果集合
  14. if (resList != null && resList.size() > 0) {
  15. System.out.println(resList);
  16. for (int i = 0; i < Integer.parseInt(validationParameter.getAttachCount()); i++) {
  17. m = i + 1; //attach次数
  18. String delayStr = resList.get(i).trim(); // 去除可能的空格
  19. if (!delayStr.equalsIgnoreCase("\"error\"")) {
  20. try {
  21. int delay = Integer.parseInt(delayStr);
  22. successCount++;
  23. totalDelay += delay;
  24. countDelayed++;
  25. // 更新最小和最大时延
  26. if (delay < minDelay) {
  27. minDelay = delay;
  28. }
  29. if (delay > maxDelay) {
  30. maxDelay = delay;
  31. }
  32. } catch (NumberFormatException e) {
  33. System.err.println("无效的数值格式: " + delayStr);
  34. }
  35. }else {
  36. errorCount++;
  37. }
  38. }
  39. // 计算平均时延
  40. double avgDelay = (countDelayed == 0) ? 0 : (double) totalDelay / countDelayed;
  41. // 构建包含统计信息的字符串
  42. // 构建包含统计信息的字符串
  43. if (errorCount != 0){
  44. s = "attach次数为" + m + ",error次数为" + errorCount +",最小时延为" + minDelay + "ms,最大时延为" + maxDelay + "ms,平均时延为" + String.format("%.2f", avgDelay) + "ms";
  45. }else {
  46. s = "attach次数为" + m + ",最小时延为" + minDelay + "ms,最大时延为" + maxDelay + "ms,平均时延为" + String.format("%.2f", avgDelay) + "ms";
  47. }
  48. //计算成功率
  49. Double successRate = successCount / Integer.parseInt(validationParameter.getAttachCount());
  50. successRate = successRate * 100;
  51. businessValidationDetails.setAttachSuccessRate(successRate.toString());
  52. //判断成功率是否正常
  53. if (Integer.parseInt(validationParameter.getAttachSuccessRate()) > successRate) {
  54. //成功率异常
  55. // 告警且推送
  56. //创建告警记录对象
  57. String alarmContent = deviceInfo.getName() + "设备的attach业务验证成功率为" + successRate + "%," + "低于设定的标准值";
  58. AlarmRecord alarmRecord = new AlarmRecord();
  59. alarmRecord.setAlarmType("6");
  60. alarmRecord.setAlarmTime(getCurrentDay());
  61. alarmRecord.setDeviceId(deviceInfo.getId());
  62. alarmRecord.setAlarmContent(alarmContent);
  63. alarmRecordService.save(alarmRecord);
  64. //推送到系统公告中
  65. sendSysAnnouncement(userList, "attach业务验证", alarmContent);
  66. businessValidationDetails.setSuccessState("0");
  67. } else {
  68. //成功率正常
  69. businessValidationDetails.setSuccessState("1");
  70. }
  71. //判断延时是否正常
  72. //计算平均延时
  73. double avgDelayed = countDelayed / successCount;
  74. if (avgDelayed > Double.parseDouble(validationParameter.getAttachDelayed())) {
  75. //延时异常
  76. //告警且推送
  77. String alarmContent = deviceInfo.getName() + "设备的attach业务验证平均时延为" + avgDelayed + "ms,低于设定的标准值";
  78. AlarmRecord alarmRecord = new AlarmRecord();
  79. alarmRecord.setAlarmType("6");
  80. alarmRecord.setAlarmTime(getCurrentDay());
  81. alarmRecord.setDeviceId(deviceInfo.getId());
  82. alarmRecord.setAlarmContent(alarmContent);
  83. alarmRecordService.save(alarmRecord);
  84. //推送到系统公告中
  85. sendSysAnnouncement(userList, "attach业务验证", alarmContent);
  86. businessValidationDetails.setTimeDelayState("0");
  87. } else {
  88. //延时正常
  89. businessValidationDetails.setTimeDelayState("1");
  90. }
  91. }
  92. return s;
  93. }**粗体文本**
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注