1

我有以下结构的文件:

ctrl_interface=/data/misc/wifi/sockets
driver_param=use_p2p_group_interface=1
update_config=1
device_name=P580_ROW
manufacturer=LENOVO
model_name=Lenovo 
model_number=Lenov
serial_number=hjhjh7
device_type=10-0050F204-5
os_version=01020300
config_methods=physical_display virtual_push_button
p2p_no_group_iface=1

network={
    ssid="test1"
    psk="154695"
    key_mgmt=WPA-PSK
    sim_slot="-1"
    imsi="none"
    priority=1
}

network={
    ssid="SSID2"
    psk="test123456"
    key_mgmt=WPA-PSK
    sim_slot="-1"
    imsi="none"
    priority=19
}

我想问一下,如何使用正则表达式(模式和匹配器)或 Java 中的类似方法获取具有可访问属性的网络对象的数组列表(但也可以是 JSON)?

我尝试使用服务 http://txt2re.com/找到解决方案, 但没有运气获得具有可访问属性的对象。

非常感谢您的任何建议。

4

2 回答 2

2

如果您不知道属性的名称及其数量,您可以使用基于\G运算符的正则表达式:

String str = "<<YOUR_STRING>>";
Pattern ptrn = Pattern.compile("(?<new>network=\\{|(?!^)\\G)\\s*(?<key>\\w+)=\"?(?<value>[^\"\n]+)\"?");
Matcher matcher = ptrn.matcher(str);
int count = 1;
while (matcher.find()) {
    if (matcher.group("new") != null && matcher.group("new").length() > 0) {
        System.out.println("New Network: " + count);
        count += 1;
    }
    System.out.println(matcher.group("key") + ":\"" + matcher.group("value").trim() + "\"");
}

IDEONE 演示

正则表达式 - (?<new>network=\{|(?!^)\G)\s*(?<key>\w+)="?(?<value>[^"\n]+)"?- 匹配:

  • (?<new>network=\{|(?!^)\G)-network={或上一场成功比赛的结束(组“新”)
  • \s*- 可选空格
  • (?<key>\w+)- 1 个或多个字母数字符号(组“键”)
  • ="?- 文字=和可选"
  • (?<value>[^"\n]+)"- 除换行符以外的 1 个或多个字符(组“值”)
  • "?- 一个可选的"
于 2015-09-07T19:37:15.660 回答
1

这是一个工作示例。尝试一下。

public class Test {

    private static final String REGEXP = "network=\\{\\n\\s*ssid=\"(?<ssid>.*)\"\\n\\s*psk=\"(?<psk>.*)\"\\n\\s*key_mgmt=(?<keymgmt>.*)\\n\\s*sim_slot=\"(?<simslot>.*)\"\\n\\s*imsi=\"(?<imsi>.*)\"\\n\\s*priority=(?<priority>.*)\\n}";
    private static final String DATA = "ctrl_interface=/data/misc/wifi/sockets\n" +
            "driver_param=use_p2p_group_interface=1\n" +
            "update_config=1\n" +
            "device_name=P580_ROW\n" +
            "manufacturer=LENOVO\n" +
            "model_name=Lenovo \n" +
            "model_number=Lenov\n" +
            "serial_number=hjhjh7\n" +
            "device_type=10-0050F204-5\n" +
            "os_version=01020300\n" +
            "config_methods=physical_display virtual_push_button\n" +
            "p2p_no_group_iface=1\n" +
            "\n" +
            "network={\n" +
            "    ssid=\"test1\"\n" +
            "    psk=\"154695\"\n" +
            "    key_mgmt=WPA-PSK\n" +
            "    sim_slot=\"-1\"\n" +
            "    imsi=\"none\"\n" +
            "    priority=1\n" +
            "}\n" +
            "\n" +
            "network={\n" +
            "    ssid=\"SSID2\"\n" +
            "    psk=\"test123456\"\n" +
            "    key_mgmt=WPA-PSK\n" +
            "    sim_slot=\"-1\"\n" +
            "    imsi=\"none\"\n" +
            "    priority=19\n" +
            "}";

    public static void main(String[] args){
        final Pattern pattern = Pattern.compile(REGEXP);
        final Matcher matcher = pattern.matcher(DATA);

        while(matcher.find()){
            System.out.println("New network");
            System.out.println("-----------");
            System.out.println("SSID: "+matcher.group("ssid"));
            System.out.println("PSK: "+matcher.group("psk"));
            System.out.println("KEY MGMT: "+matcher.group("keymgmt"));
            System.out.println("SIM SLOT: "+matcher.group("simslot"));
            System.out.println("IMSI: "+matcher.group("imsi"));
            System.out.println("PRIORITY: "+matcher.group("priority"));
        }
    }
}

我在 RegExp 中效率不高。我认为这种方法很脏,会有比这更好的解决方案。(工作区:https ://regex101.com/r/nA8fD1/1 )

于 2015-09-07T19:13:48.977 回答