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();
}