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

论坛 期权论坛 脚本     
匿名技术用户   2020-12-28 04:41   33   0
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 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP