我有以下字符串輸入(從netstat -a
命令):解析沒有忽略空格 - Java的
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ] DGRAM 11453 /run/systemd/shutdownd
unix 2 [ ] DGRAM 7644 /run/systemd/notify
unix 2 [ ] DGRAM 7646 /run/systemd/cgroups-agent
unix 5 [ ] DGRAM 7657 /run/systemd/journal/socket
unix 14 [ ] DGRAM 7659 /dev/log
unix 3 [ ] STREAM CONNECTED 16620
unix 3 [ ] STREAM CONNECTED 16621
同時我試圖分析上面的字符串:
// lines is an array representing each line above
for (int i = 0; i < lines.length; i++) {
String[] tokens = lines[i].split("\\s+");
}
我想將tokens
作爲7個條目[Proto, RefCnt, Flag, Type, State, I-Node, Path]
的數組。相反,我得到排除Flags
下架和空State
數組:我怎樣才能解決我的正則表達式來產生正確輸出的
["unix", "2", "[", "]", "DGRAM", "11453", "/run/systemd/shutdownd"]
代替
["unix", "2", "[]", "DGRAM", "", "11453", "/run/systemd/shutdownd"]
?
使用lookarounds' (?<!\ [)\\ s +(?!\))' – revo
@revo即使在lookaround中也有一個值缺失(對於狀態,數組長度將是6而不是7) – cybertextron