我有以下字符串输入(来自netstat -a
命令):
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"]
如何修复正则表达式以产生正确的输出?
答案 0 :(得分:1)
您需要将正则表达式中的最小空格长度设置为2,尝试拆分如下:
String[] tokens = lines[i].split("\\s{2,16}+");
或者像@revo建议使用lookarounds,如下所示:
String[] tokens = lines[i].split("(?<!\\[)\\s{2,16}+(?!\\])");