利用正则表达式:找到一段字符串中所有的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-zA-Z0-9._%+-]+@(?!.*\.\..*)[a-zA-Z0-9.-]+\.[a-zA-Z]2,4
以上是关于利用正则表达式:找到一段字符串中所有的IP地址和Email地址的主要内容,如果未能解决你的问题,请参考以下文章