简体   繁体   中英

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? 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

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:

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 .

You can use the below code for split the IP Address.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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