LV NAME TYPE LPs PPs PVs LV STATE MOUNT POINT
hd5 boot 1 1 1 closed/syncd N/A
hd6 paging 17 17 1 open/syncd N/A
...
提取 LV NAME: hd5、LV NAME: hd6 等格式。
String input = "..."; // 完整多行字符串
// 1. 統一換行符
input = input.replaceAll("\\r|\\n", "##");
// 2. 提取表頭第一列關鍵字 "LV NAME"
String header = input.replaceAll("(\\w+\\s+\\w+).+", "$1"); // "LV NAME"
// 3. 匹配每行第一列(數字字母組合)
Pattern p = Pattern.compile("##(\\w+?\\d+)");
Matcher m = p.matcher(input);
StringBuilder sb = new StringBuilder();
while (m.find()) {
sb.append(header).append(": ").append(m.group(1)).append(", ");
}
if (sb.length() > 0) sb.setLength(sb.length()-2);
System.out.println(sb); // LV NAME: hd5, LV NAME: hd6, ...
##(\\w+?\\d+):匹配 ## 後的單詞字符(非貪婪)直到數字結尾,即每行第一列。(\\w+\\s+\\w+):匹配表頭前兩個單詞 LV NAME。