Java:字符串类简单的正则表达式

 1 class Test
 2 {
 3     public static void main(String[] args)
 4     {
 5         String str = "xia..as....yuan.com";
 6         
 7         //判断
 8         boolean b1 = str.endsWith(".com");
 9         boolean b2 = str.startsWith("xia");
10         System.out.println(b1);
11         System.out.println(b2);
12         
13         //切割
14         String regex = "\\.+";
15         String[] arr = str.split(regex);
16         for(String s:arr)
17         {
18             System.out.println(s);
19         }
20 
21         //替换
22         String regex1 = "\\.+";
23         String s1 = str.replaceAll(regex1,"*");
24         System.out.println(s1);
25 
26         //多个标符变一个
27         String regex2 = "(.)\\1+";
28         String s2 = str.replaceAll(regex2,"$1");
29         System.out.println(s2);
30     }
31 }

//对于匹配:可以查看API,找到匹配器Pattern以及match即可

posted @ 2015-08-11 21:02  XYQ全哥  阅读(200)  评论(0编辑  收藏  举报