Java使用正则表达式从字符串中提取 IP 地址
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class ExtractIP {public static void main(String[] args) {String ipString = "1.1.1.1 2.2.2.2";String IPADDRESS_PATTERN = "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";Pattern pattern = Pattern.compile(IPADDRESS_PATTERN);Matcher matcher = pattern.matcher(ipString);while (matcher.find()) {System.out.println(matcher.group());}}}
/*** This class provides a variety of basic utility methods that are not* dependent on any other classes within the org.jamwiki package structure.*/
public class Utilities {private static Pattern VALID_IPV4_PATTERN = null;private static Pattern VALID_IPV6_PATTERN = null;private static final String ipv4Pattern = "(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])";private static final String ipv6Pattern = "([0-9a-f]{1,4}:){7}([0-9a-f]){1,4}";static {try {VALID_IPV4_PATTERN = Pattern.compile(ipv4Pattern, Pattern.CASE_INSENSITIVE);VALID_IPV6_PATTERN = Pattern.compile(ipv6Pattern, Pattern.CASE_INSENSITIVE);} catch (PatternSyntaxException e) {//logger.severe("Unable to compile pattern", e);}}/*** Determine if the given string is a valid IPv4 or IPv6 address. This method* uses pattern matching to see if the given string could be a valid IP address.** @param ipAddress A string that is to be examined to verify whether or not* it could be a valid IP address.* @return <code>true</code> if the string is a value that is a valid IP address,* <code>false</code> otherwise.*/public static boolean isIpAddress(String ipAddress) {Matcher m1 = Utilities.VALID_IPV4_PATTERN.matcher(ipAddress);if (m1.matches()) {return true;}Matcher m2 = Utilities.VALID_IPV6_PATTERN.matcher(ipAddress);return m2.matches();}
}
Java使用正则表达式从字符串中提取 IP 地址相关推荐
- 【C语言】从字符串中提取IP地址最简洁的方法
首先先看一下判断字符串是否为IP地址(IPv4地址是否合法)的方法: ([C语言]判断字符串是否为IP地址(IPv4地址是否合法)) #include <stdio.h> #include ...
- 字符串双引号表示c语言,c语言中单引号和双引号的区别(顺利解决从字符串中提取IP地址的困惑)...
c语言中的单引号和双引号可是有很大区别的,使用之前一定要了解他们之间到底有什么不同,下面小编就给大家详细的介绍一下吧,对此还不是很了解的朋友可以过来参考下 问题:从c++文件中将std:string转 ...
- 字符串中提取IP地址
// 带端口号的IP地址 QStringList getIpAddressAndPort(const QString & strText) {QStringList strList;QRegE ...
- C语言从一段字符串中提取IP地址的方法
1. 需求 当前待解析字符串格式为: +CIFSR:STAIP,<Station IP address> +CIFSR:STAMAC,<Station MAC address> ...
- python正则表达式提取数字比较好_python正则表达式从字符串中提取数字的思路详解...
python从字符串中提取数字 使用正则表达式,用法如下: ## 总结 ## ^ 匹配字符串的开始. ## $ 匹配字符串的结尾. ## \b 匹配一个单词的边界. ## \d 匹配任意数字. ## ...
- 从字符串中提取IP子串(C语言)
1 目标 ① 从字符串中提取IP子串 ② 验证IP字符串是否合法(IPV4) 2 代码 ① ip地址提取 /*** @brief : IPv4地址提取* @param in_ip : 待提取字符串* ...
- 字符串中查找IP地址的正则表达式
字符串中查找IP地址的正则表达式: IP地址:(2[0-4]\d|25[0-5]|[01]\d\d|\d\d|\d)\.(2[0-4]\d|25[0-5]|[01]\d\d|\d\d|\d)\.(2[ ...
- java用正则表达式判断字符串中是否仅包含英文字母、数字和汉字_灵思致远Leansmall的博客-CSDN博客_java判断字符串只包含数字字母
import java.util.regex.Matcher; import java.util.regex.Pattern;public class StrValidate {// 纯数字priva ...
- 【R语言】正则表达式 从字符串中提取数字
R语言 正则表达式 从字符串中提取数字 pattern <- "(?<ID>[0-9]+)" result <- regexpr(pattern= patt ...
最新文章
- cocos2x (c++/lua) spine 文件的预加载
- 2020华为推迟发布鸿蒙战略,早新闻:华为5nm麒麟芯片延期,确认新版鸿蒙将发...
- 记录一次血崩式的CPLD不稳定 bug经验
- Matlab循环读取txt文件并对其中数据进行计算最后导出为excel
- JS双向数据绑定的原理介绍
- 单词嵌入_单词嵌入与单词袋:推荐系统的奇怪案例
- vb 获取系统声音的电平_质量好的背景音乐系统套装效果图
- 在MFC程序中增加控制台窗口
- 关于AD域账户和组的管理技巧
- spring框架做全局异常捕获_springboot springmvc抛出全局异常的解决方法
- 六问六答理解ForkJoin原理
- PyCharm中配置模板
- vb mysql 实例教程_在VB.NET应用中使用MySQL的方法
- OKR最新模板推荐,生产研发团队如何使用OKR工具?
- Bazinga HDU - 5510
- JS 实现点击按钮实现超链接功能
- RationalDMIS基于CAD的编程测量
- Hadoop 表和字段
- 【无关技术·朋友圈朝花朝拾】月相
- 计算机中文字体的独创性,计算机中文字体中单字的著作权保护研究(2)