利用正则表达式:找到一段字符串中所有的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); 
		 }
	
}

********************************************************************************************************************************************************************************

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}

参考2

代码示例:

 String regexString="\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
 Pattern p = Pattern.compile(regexString);
 String parentString="abdk d2.2d9(,dsf ]t20,2.33.1.8yff";
 Matcher m = p.matcher(parentString);
 boolean result = m.find();
 while(result) {
  System.out.println(m.group());
  result = m.find();
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值