You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Java从Shell命令输出字符串提取指定键值对存入Map

从Shell输出字符串中提取指定键值对到Java Map的实现方案

首先明确需求:给定一段Shell命令输出的字符串,需要提取bluetooth_mac=45h6kuu、build_type=BFD、tdVersion=1.2、seloger_mac=F4:0E:83:35:E8:D0、france_mac=fjdjjjgj这几个指定键值对,存入Java的Map<String, String>集合中。

先把输入的字符串示例放出来:

String shellOutput = "dat /var/france.log exit bluetooth_mac=45h6kuu franceIP=testMarmiton build_type=BFD france_mac=F4:0E:83:35:E8:D1 seloger_mac=F4:0E:83:35:E8:D0 tdVersion=1.2 td_number=45G67j france_mac=fjdjjjgj logo_mac=tiuyiiy logout Connection to testMarmiton closed. Disconnected channel and session";

我给你两种实用的实现思路,都是Java里常用的方法,你可以根据自己的习惯选择:

方法一:正则表达式匹配(精准定位指定键)

这种方法适合精准定位目标键,利用正则匹配(指定键)=([^ ]+)的格式,一次性找出所有符合要求的键值对,效率较高。

代码示例:

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ShellOutputParser {
    public static void main(String[] args) {
        String shellOutput = "dat /var/france.log exit bluetooth_mac=45h6kuu franceIP=testMarmiton build_type=BFD france_mac=F4:0E:83:35:E8:D1 seloger_mac=F4:0E:83:35:E8:D0 tdVersion=1.2 td_number=45G67j france_mac=fjdjjjgj logo_mac=tiuyiiy logout Connection to testMarmiton closed. Disconnected channel and session";
        
        // 定义需要提取的键列表
        String[] targetKeys = {"bluetooth_mac", "build_type", "tdVersion", "seloger_mac", "france_mac"};
        Map<String, String> resultMap = new HashMap<>();
        
        // 构造正则:匹配指定键的key=value格式,value是直到下一个空格的内容
        String regex = String.join("|", targetKeys).replace(".", "\\.") + "=([^ ]+)";
        Pattern pattern = Pattern.compile("(" + regex + ")");
        Matcher matcher = pattern.matcher(shellOutput);
        
        while (matcher.find()) {
            String keyValuePair = matcher.group(1);
            String[] parts = keyValuePair.split("=", 2); // 只分割一次,避免value含=的情况
            if (parts.length == 2) {
                resultMap.put(parts[0], parts[1]);
            }
        }
        
        // 验证结果
        resultMap.forEach((key, value) -> System.out.println(key + " = " + value));
    }
}

说明:因为tdVersion里的.是正则特殊字符,所以做了转义处理;split("=", 2)确保如果value意外包含=也不会被错误分割。另外,后面匹配到的同键值会覆盖前面的,正好满足你取最后一个france_mac值的需求。

方法二:分割字符串遍历筛选(直观易懂)

这种方法更直观,把整个字符串按空格分割成元素,遍历每个元素判断是否属于目标键的键值对,符合条件就存入Map。

代码示例:

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class ShellOutputParser {
    public static void main(String[] args) {
        String shellOutput = "dat /var/france.log exit bluetooth_mac=45h6kuu franceIP=testMarmiton build_type=BFD france_mac=F4:0E:83:35:E8:D1 seloger_mac=F4:0E:83:35:E8:D0 tdVersion=1.2 td_number=45G67j france_mac=fjdjjjgj logo_mac=tiuyiiy logout Connection to testMarmiton closed. Disconnected channel and session";
        
        // 定义目标键集合,提升判断效率
        Set<String> targetKeys = new HashSet<>();
        targetKeys.add("bluetooth_mac");
        targetKeys.add("build_type");
        targetKeys.add("tdVersion");
        targetKeys.add("seloger_mac");
        targetKeys.add("france_mac");
        
        Map<String, String> resultMap = new HashMap<>();
        
        // 按空格分割字符串
        String[] elements = shellOutput.split(" ");
        for (String element : elements) {
            if (element.contains("=")) {
                String[] parts = element.split("=", 2);
                if (parts.length == 2 && targetKeys.contains(parts[0])) {
                    resultMap.put(parts[0], parts[1]);
                }
            }
        }
        
        // 验证结果
        resultMap.forEach((key, value) -> System.out.println(key + " = " + value));
    }
}

说明:用HashSet存储目标键是为了让contains判断更高效;同样,后面的同键值会覆盖前面的,满足取最后一个france_mac的需求。

两种方法都能实现需求,正则法适合字符串较长、目标键明确的场景,遍历分割法则更直观,容易调试。

内容的提问来源于stack exchange,提问作者divz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:24:59