Teach you how to use regular expressions to sort IPs
正则表达式在我们开发中必不可少,本文我们主要介绍了利用正则表达对IP进行排序的实现代码。
1、补零,使得可以按照字符串顺序进行比较。
2、截取保留后三位(ip地址最多就3位)。
3、利用Arrays.sort()方法对截取的字符串进行排序。。
4、去除多余的0,回复ip原样。
5、实现代码:
package IPSort; import java.util.Arrays; /** * 利用正则表达对IP进行排序,分四步 * @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、补零------"); for (int i = 0; i < ips.length; i++) { ips[i] = ips[i].replaceAll("(\\d+)", "00$1"); System.out.println(ips[i]); } System.out.println("------2、截取------"); for (int i = 0; i < ips.length; i++) { ips[i] = ips[i].replaceAll("0*(\\d{3})", "$1"); System.out.println(ips[i]); } System.out.println("------3、排序------"); Arrays.sort(ips); for (int i = 0; i < ips.length; i++) { System.out.println(ips[i]); } System.out.println("------4、去零------"); for (int i = 0; i < ips.length; i++) { ips[i] = ips[i].replaceAll("0*(\\d+)", "$1"); System.out.println(ips[i]); } } }
6、运行结果:
------原IP地址------ 10.2.4.23 192.168.1.2 173.68.46.65 191.158.6.2 9.2.4.23 ------1、加零,按字符串顺序比较------ 0010.002.004.0023 00192.00168.001.002 00173.0068.0046.0065 00191.00158.006.002 009.002.004.0023 ------2、截取,保留三位------ 010.002.004.023 192.168.001.002 173.068.046.065 191.158.006.002 009.002.004.023 ------3、排序------ 009.002.004.023 010.002.004.023 173.068.046.065 191.158.006.002 192.168.001.002 ------4、去零------ 9.2.4.23 10.2.4.23 173.68.46.65 191.158.6.2 192.168.1.2
以上内容就是利用正则表达对IP进行排序的实现代码,希望能帮助到大家。
相关推荐:
The above is the detailed content of Teach you how to use regular expressions to sort IPs. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use GitLab for project document management 1. Background introduction In the software development process, project documents are very important information. They can not only help the development team understand the needs and design of the project, but also provide reference to the testing team and customers. In order to facilitate version control and team collaboration of project documents, we can use GitLab for project document management. GitLab is a version control system based on Git. In addition to supporting code management, it can also manage project documents. 2. GitLab environment setup First, I

Golang regular expressions use the pipe character | to match multiple words or strings, separating each option as a logical OR expression. For example: matches "fox" or "dog": fox|dog matches "quick", "brown" or "lazy": (quick|brown|lazy) matches "Go", "Python" or "Java": Go|Python |Java matches words or 4-digit zip codes: ([a-zA

PHP regular expressions are a powerful tool for text processing and conversion. It can effectively manage text information by parsing text content and replacing or intercepting it according to specific patterns. Among them, a common application of regular expressions is to replace strings starting with specific characters. We will explain this as follows

How to remove Chinese in PHP using regular expressions: 1. Create a PHP sample file; 2. Define a string containing Chinese and English; 3. Use "preg_replace('/([\x80-\xff]*)/i', '',$a);" The regular method can remove Chinese characters from the query results.

As a world-renowned short video social platform, Douyin has won the favor of a large number of users with its unique personalized recommendation algorithm. This article will delve into the value and principles of Douyin video recommendation to help readers better understand and make full use of this feature. 1. What is Douyin recommended video? Douyin recommended video uses intelligent recommendation algorithms to filter and push personalized video content to users based on their interests and behavioral habits. The Douyin platform analyzes users' viewing history, like and comment behavior, sharing records and other data to select and recommend videos that best suit users' tastes from a huge video library. This personalized recommendation system not only improves user experience, but also helps users discover more video content that matches their preferences, thereby enhancing user stickiness and retention rate. at this

In this article, we will learn how to remove HTML tags and extract plain text content from HTML strings using PHP regular expressions. To demonstrate how to remove HTML tags, let's first define a string containing HTML tags.

Website security has attracted more and more attention, and using the HTTPS protocol to ensure the security of data transmission has become an important part of current website development. In PHP development, how to use regular expressions to verify whether the URL is HTTPS protocol? here we come to find out. Regular expression Regular expression is an expression used to describe rules. It is a powerful tool for processing text and is widely used in text matching, search and replacement. In PHP development, we can use regular expressions to match http in the URL

With the continuous development of computer hardware, the CPU cores in the processor no longer increase the clock frequency individually, but increase the number of cores. This raises an obvious question: How to get the most out of these cores? One solution is through parallel programming, which involves executing multiple tasks simultaneously to fully utilize the CPU cores. This is a unique feature of the Go language. It is a language designed specifically for concurrent programming. In this article, we will explore how to leverage Go language for concurrent programming. Coroutines First, we need to understand
