返回
正则表达式教程:IPv4地址与端口匹配
正则表达式
2024-02-28 16:04:39
一、正则解释
提供的正则表达式:
/^((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])(?::(?:[0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))?$/
逐段分析:
^
:匹配字符串的开始。(
: 开始一个捕获组,用于捕获IPv4地址。(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])
: 匹配单个IPv4地址段,范围从0到255。.
:匹配句点,作为IPv4地址段之间的分隔符。){3}
:重复上述模式三次,匹配IPv4地址的所有四个段。)
: 结束捕获组。(?::)
: 可选地匹配冒号,表示端口部分的开始。(?:
:开始一个非捕获组,用于匹配端口。[0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])
: 匹配端口号,范围从0到65535。)
: 结束非捕获组。$
: 匹配字符串的结束。
二、使用场景
此正则表达式可用于:
- 验证IPv4地址的有效性。
- 从字符串中提取IPv4地址和端口号。
- 过滤包含特定IPv4地址和端口号的日志或数据。
三、代码示例
JavaScript
const regex = /^((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])(?::(?:[0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))?$/;
const input = "172.16.0.0:8080";
const match = regex.exec(input);
if (match) {
const ipAddress = match[1];
const port = match[2] || "80";
console.log(`IPv4 Address: ${ipAddress}, Port: ${port}`);
} else {
console.log("Invalid IPv4 address or port format.");
}
Java
import java.util.regex.Pattern;
public class IPv4PortMatcher {
private static final Pattern PATTERN = Pattern.compile("^((\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])(?::(?:[0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))?import java.util.regex.Pattern;
public class IPv4PortMatcher {
private static final Pattern PATTERN = Pattern.compile("^((\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])(?::(?:[0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))?$");
public static void main(String[] args) {
String input = "172.16.0.0:8080";
Matcher matcher = PATTERN.matcher(input);
if (matcher.find()) {
String ipAddress = matcher.group(1);
String port = matcher.group(2);
System.out.println("IPv4 Address: " + ipAddress + ", Port: " + port);
} else {
System.out.println("Invalid IPv4 address or port format.");
}
}
}
quot;);
public static void main(String[] args) {
String input = "172.16.0.0:8080";
Matcher matcher = PATTERN.matcher(input);
if (matcher.find()) {
String ipAddress = matcher.group(1);
String port = matcher.group(2);
System.out.println("IPv4 Address: " + ipAddress + ", Port: " + port);
} else {
System.out.println("Invalid IPv4 address or port format.");
}
}
}
PHP
<?php
$regex = "/^((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])(?::(?:[0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))?$/";
$input = "172.16.0.0:8080";
if (preg_match($regex, $input, $matches)) {
$ipAddress = $matches[1];
$port = $matches[2] ?? "80";
echo "IPv4 Address: $ipAddress, Port: $port\n";
} else {
echo "Invalid IPv4 address or port format.\n";
}
?>
Python
import re
regex = r"^((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])(?::(?:[0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))?import re
regex = r"^((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])(?::(?:[0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))?$"
input = "172.16.0.0:8080"
match = re.search(regex, input)
if match:
ipAddress = match.group(1)
port = match.group(2) or "80"
print(f"IPv4 Address: {ipAddress}, Port: {port}")
else:
print("Invalid IPv4 address or port format.")
quot;
input = "172.16.0.0:8080"
match = re.search(regex, input)
if match:
ipAddress = match.group(1)
port = match.group(2) or "80"
print(f"IPv4 Address: {ipAddress}, Port: {port}")
else:
print("Invalid IPv4 address or port format.")