{"title":"Java从字符串中提取IP地址 - 实用示例代码","description":"本文提供了一个Java示例代码,使用正则表达式从字符串中提取IP地址,适用于需要从jdbc连接字符串等类似文本中获取IP地址的场景。","keywords":"java, ip地址, 正则表达式, 字符串提取, jdbc, 连接字符串, 示例代码","content":"\u003ccode\u003eimport java.util.regex.Matcher;\nimport java.util.regex.Pattern;\n\npublic class IPAddressExtractor {\n public static void main(String[] args) {\n String str = "jdbc:dm://localhost:5236/PLS";\n String ipAddress = extractIPAddress(str);\n System.out.println(ipAddress);\n }\n\n public static String extractIPAddress(String str) {\n String ipAddress = "";\n\n // 定义IP地址的正则表达式\n String regex = "\b(?:\d{1,3}.){3}\d{1,3}\b";\n\n Pattern pattern = Pattern.compile(regex);\n Matcher matcher = pattern.matcher(str);\n\n if (matcher.find()) {\n ipAddress = matcher.group();\n }\n\n return ipAddress;\n }\n}\n\u003c/code\u003e\n\n输出结果为:localhost


原文地址: http://www.cveoy.top/t/topic/p8LA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录