2

我想使用返回给我 ( ssid , psk , proiority ) 作为 和与每个键关联的的模式。如果有办法理解我必须使用的表达方式

Pattern.compile("(\\w+?):(\\w+?),")

回复我。

这是我的字符串

network={
     ssid="name"
     psk="password"
     key_mgmt=WPA-PSK
     priority=1
}

network={
     ssid="another name"
     psk="another password"
     key_mgmt=WPA-PSK
     priority=1
}
4

1 回答 1

1

这是一个丑陋的解决方案:

我将您的文本输入放入一个名为的文件中file.txt并通过 BufferedReader 读取它

public static void main(String[] args) throws Exception {
        File f = new File("src/test/file.txt");
        BufferedReader read = new BufferedReader(new FileReader(f));
        String temp;
        String result = "";
        while ((temp = read.readLine()) != null) {
            result += temp;
        }
        result = result.replaceAll("\\{", " ");
        result = result.replaceAll("\\}", " ");
        result = result.replaceAll("network=", " ");
        result = result.trim();

        String[] list = result.split("[ ]+(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
        for (String s : list) {
            s = s.trim();
            Pattern p = Pattern.compile("(.*)=(.*)");
            Matcher m = p.matcher(s);
            while (m.find()) {
                System.out.println("Key: " + m.group(1));
                System.out.println("Value: " + m.group(2));
            }
        }

    }

这是我的输出:

Key: ssid
Value: "name"
Key: psk
Value: "password"
Key: key_mgmt
Value: WPA-PSK
Key: priority
Value: 1
Key: ssid
Value: "another name"
Key: psk
Value: "another password"
Key: key_mgmt
Value: WPA-PSK
Key: priority
Value: 1
于 2012-09-25T14:41:34.757 回答