1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| import java.util.*; import java.util.regex.*; class Regex { public static void main(String[] args) { test3(); } public static void test3(){ String mail = "[email protected]"; String regex = "[a-zA-Z0-9_]+@[a-zA-Z0-9_]+\\.[a-zA-Z]{2,3}"; System.out.println(mail.matches(regex)); } public static void test2(){ String ip_str = "10.21.130.111 12.210.15.1 15.12.12.1 152.152.15.151 13.151.15.1"; ip_str = ip_str.replaceAll("(\\d+)","00$1"); System.out.println(ip_str); ip_str = ip_str.replaceAll("0*(\\d{3})","$1"); System.out.println(ip_str); String[] ips = {"10.21.130.111","12.210.15.1","15.12.12.1","152.152.15.151","13.151.15.1"}; } public static void test1(){ String str = "我我我我....我我我...要.要.要要要要..学学学学....学编编编...编编程..程...程程....程程"; str = str.replaceAll("\\.+",""); System.out.println(str); str = str.replaceAll("(.)\\1+","$1"); System.out.println(str); } public static void f4(){ String str = "da jia hao ,ming tian bu fang jia!";
String regex = "\\b[a-z]{3}\\b"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(str); while(m.find()){ System.out.print(m.group());System.out.println(m.start()+":"+m.end()); }
} public static void f3(){ String str = "xiaoqiangttttzhangsanmmmmmzhaoliu"; str = str.replaceAll("(.)\\1+","$1"); System.out.println(str); String tel = "15700001111"; tel = tel.replaceAll("(\\d{3})(\\d{4})(\\d{4})","$1****$3"); System.out.println(tel); } public static void f2(){ String str = "xiaoqiangttttzhangsanmmmmmzhaoliu"; String[] name = str.split("(.)\\1+"); for(String s:name){ System.out.println(s); } } public static void f1(){ String regex = "1[358]\\d{9}"; String s = "211215121"; Scanner sc = new Scanner(System.in); while((s=sc.nextLine())!=null){ System.out.println(s.matches(regex)); } } }
|