为什么在我的正则表达式中从错误的区域捕获组? (Java 7)

时间:2013-01-07 04:03:17

标签: java regex

正在从错误的区域捕获数据。它从eth1而不是eth0捕获ip地址字段和子网。我不明白为什么会这样。我也试过了matcher.find(0)但得到了相同的结果。

String[] dataNames = new String[]{"eth0Ip", "eth0Subnet"}
dataExtractionPattern = Pattern.compile("eth0 .*inet (?<eth0Ip>\\S+)  mask (?<eth0Subnet>\\S+)",Pattern.DOTALL);

Matcher matcher = dataExtractionPattern.matcher(receivedDataString);
if (matcher.find()) {
    for (String key : dataNames) {
        String dataValue;
        dataValue = matcher.group(key);
        extractedData.put(key, dataValue);
    }
    hasData = true;
}

输入字符串是:

lo0     Link type:Local loopback  Queue:none
        inet 127.0.0.1  mask 255.255.255.255
        UP RUNNING LOOPBACK
        MTU:1500  metric:1  VR:0
        RX packets:4 mcast:0 errors:0 dropped:1
        TX packets:4 mcast:0 errors:0
        collisions:0 unsupported proto:0
        RX bytes:172  TX bytes:172

eth0    Link type:Ethernet  HWaddr 00:25:f2:5e:9c:34  Queue:none
        inet 10.1.2.2  mask 10.1.2.1  broadcast 255.255.255.254
        RUNNING BROADCAST
        MTU:1000  metric:1  VR:0
        RX packets:0 mcast:0 errors:0 dropped:0
        TX packets:0 mcast:0 errors:0
        collisions:0 unsupported proto:0
        RX bytes:0  TX bytes:0

eth1    Link type:Ethernet  HWaddr 00:25:f2:5e:9c:33  Queue:none
        inet 192.168.200.51  mask 255.255.255.0  broadcast 192.168.200.255
        UP RUNNING BROADCAST
        MTU:1500  metric:1  VR:0
        RX packets:0 mcast:0 errors:0 dropped:0
        TX packets:0 mcast:0 errors:0
        collisions:0 unsupported proto:0
        RX bytes:0  TX bytes:0

对于eth0 ip,它错误地捕获192.168.200.51并屏蔽255.255.255.0

0 个答案:

没有答案