Home Backend Development PHP Tutorial Teach you how to use regular expressions to sort IPs

Teach you how to use regular expressions to sort IPs

Dec 05, 2017 pm 02:28 PM
use regular

正则表达式在我们开发中必不可少,本文我们主要介绍了利用正则表达对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]);
 }
 }
}
Copy after login

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
Copy after login

以上内容就是利用正则表达对IP进行排序的实现代码,希望能帮助到大家。

相关推荐:

php正则表达式应用

python编译正则表达式提高效率方法详解

Python中正则表达式的知识汇总分享

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use GitLab for project document management How to use GitLab for project document management Oct 20, 2023 am 10:40 AM

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

How to match multiple words or strings using Golang regular expression? How to match multiple words or strings using Golang regular expression? May 31, 2024 am 10:32 AM

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

How to replace a string starting with something with php regular expression How to replace a string starting with something with php regular expression Mar 24, 2023 pm 02:57 PM

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 use regular expressions to remove Chinese characters in php How to use regular expressions to remove Chinese characters in php Mar 03, 2023 am 10:12 AM

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.

What does TikTok recommended video mean? How to use Douyin to recommend videos? What does TikTok recommended video mean? How to use Douyin to recommend videos? Mar 27, 2024 pm 03:01 PM

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

How to use regular matching to remove html tags in php How to use regular matching to remove html tags in php Mar 21, 2023 pm 05:17 PM

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.

How to use Go language for concurrent programming? How to use Go language for concurrent programming? Jun 10, 2023 am 10:33 AM

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

Sharing tips on using PHP regular expressions to implement Chinese replacement function Sharing tips on using PHP regular expressions to implement Chinese replacement function Mar 24, 2024 pm 05:57 PM

Sharing tips on using PHP regular expressions to implement the Chinese replacement function. In web development, we often encounter situations where Chinese content needs to be replaced. As a popular server-side scripting language, PHP provides powerful regular expression functions, which can easily realize Chinese replacement. This article will share some techniques for using regular expressions to implement Chinese substitution in PHP, and provide specific code examples. 1. Use the preg_replace function to implement Chinese replacement. The preg_replace function in PHP can be used

See all articles