这是一个丑陋的解决方案:
我将您的文本输入放入一个名为的文件中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