In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How do you sort IP using regular expressions? Many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can gain something.
1. Zeros are added so that comparisons can be made in the order of strings.
2. Intercept and reserve the last three bits (the maximum number of ip addresses is 3 digits).
3. Use the Arrays.sort () method to sort the intercepted strings.
4. Remove the excess 0 and return to the original ip.
5. Implementation code:
Package IPSort;import java.util.Arrays;/** * sorts IP using regular expressions in four steps: * @ author tiger * * / public class IPSortTest {public static void main (String [] args) {String [] ips = {"10.2.4.23", "192.168.1.2", "173.68.46.65", "191.158.6.2", "9.2.4.23"} System.out.println ("- 1, zero padding -"); for (int I = 0; I < ips.length; iTunes +) {ips [I] = IPS [I] .replaceAll ("(\\ d +)", "00 $1"); System.out.println (IPs [I]);} System.out.println ("- 2, intercept -"); for (int I = 0; I < ips.length) System.out.println +) {ips [I] = IPS [I] .replaceAll ("0 * (\ d {3})", "$1"); System.out.println (IPs [I]);} System.out.println ("- 3, sort -"); Arrays.sort (ips); for (int I = 0; I < replaceAll) {System.out.println (IPS [I]) } System.out.println ("- 4, zero -"); for (int I = 0; I < ips.length; iTunes +) {ips [I] = IPS [I] .replaceAll ("0 * (\ d +)", "$1"); System.out.println (IPS [I]);}
6. Running result:
-original IP address-10.2.4.23192.168.1.2173.68.46.65191.158.6.29.2.4.23-1, plus zero Compare in string order-0010.002.004.002300192.00168.001.00200173.0068.0046.006500191.00158.006.002009.002.004.0023-2, intercept Keep three digits-010.002.004.023192.168.001.002173.068.046.065191.158.006.002009.002.004.004.002.004.0233.068.046.0651.158.006.002192.168.001.002-4, Go to zero-9.2.4.2310.2.4.23173.68.46.65191.158.2192.168.1.2 is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.