利用正则表达式:找到一段字符串中所有的IP地址和Email地址

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用正则表达式:找到一段字符串中所有的IP地址和Email地址相关的知识,希望对你有一定的参考价值。

import java.util.Arrays;
public class RegularExpression
public static String knights = "When you feel hurt and your tears are gonna to drop.Please look up and have a look at the sky once belongs to us." +
"159.226.1.1" +
"If the sky is still vast,clouds are still clear, you shall not cry because my leave doesn't take away the world that belongs to you." +
"10.1.0.2" +
"1943426595@qq.com" +
"192.168.205.65";
public static String Regex = "^(2[0-4]\\d | 25[0-5]|[01]?\\d?[1-9])\\." +
"(2[0-4]\\d | 25[0-5] | [01]?\\d?\\d)\\." +
"(2[0-4]\\d | 25[0-5] | [01]?\\d?\\d)\\." +
"(2[0-4]\\d | 25[0-5] | [01]?\\d?\\d)$";
public static String Mail = "^[a-z]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\\.][a-z]2,3([\\.][a-z]2)?$";
public static void split(String str)
System.out.println(Arrays.toString(knights.split(str)));

public static void matches(String str)
System.out.println(knights.matches(str));


public static void main(String[] args)
matches(Regex);
matches(Mail);
split(Regex);
split(Mail);



执行结果:false
[When you feel hurt and your tears are gonna to drop.Please look up and have a look at the sky once belongs to us.159.226.1.1If the sky is still vast,clouds are still clear, you shall not cry because my leave doesn't take away the world that belongs to you.10.1.0.21943426595@qq.com192.168.205.65]重复

参考技术A 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[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)
邮件地址:[a-zA-Z0-9._%+-]+@(?!.*\.\..*)[a-zA-Z0-9.-]+\.[a-zA-Z]2,4

以上是关于利用正则表达式:找到一段字符串中所有的IP地址和Email地址的主要内容,如果未能解决你的问题,请参考以下文章

正则表达式查找除 IP 地址以外的所有 IP 地址以 172 开头

如何利用js正则表达式判断ip地址的合法性

如何用正则表达式匹配IP地址

Java正则表达式测试用例

正则表达式实例

用于从封闭字符串中提取 IP 地址和端口的简单 Java 正则表达式