简体   繁体   English

如何将字符串与IP和端口分开?

[英]How i can divide the string with IP and ports?

I have such a line: 我有这样一行:

176.32.37.27:777754.38.156.202:777751.68.208.5:7777

How can the cycle inferred from this string of IP ports by using the split function or similar? 如何使用拆分功能或类似功能从此IP端口字符串中推断出周期? To make it so: 为此:

176.32.37.27:7777 54.38.156.202:7777 51.68.208.5:7777

the ports are the same only because they have 4 digits 端口是相同的,只是因为它们有4位数字

Since you know your port's length will always be 4, you can split based on a lookbehind that takes the colon and 4 digits: 由于您知道端口的长度始终为4,因此您可以根据后面的冒号和4位数字进行拆分:

String test = "176.32.37.27:777754.38.156.202:777751.68.208.5:7777";
System.out.println(
   Arrays.toString(
       test.split("(?<=:\\d{4})")
   )
);

Output 输出量

[176.32.37.27:7777, 54.38.156.202:7777, 51.68.208.5:7777]

See also 也可以看看

Lookbehind in Java pattern API . 在Java模式API中落后。

You can use the below code for split the IP Address. 您可以使用以下代码拆分IP地址。

String str = "176.32.37.27:777754.38.156.202:777751.68.208.5:7777";

String ipAdds[] = str.split("(?<=:\\d{4})");

for (String ipAdd: ipAdds)
     System.out.println(ipAdd);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM