正则表达式学习笔记!

来源:互联网 发布:双轨直销软件开发 编辑:程序博客网 时间:2024/06/05 03:01

/*
正则表达式.
是一种规则,是一种操作字符串的规则。
1,匹配。String  matches(regex)
2,获取匹配内容。
3,切割。
4,替换。

*/

import java.util.regex.*;
import java.util.*;
class  RegexDemo
{
 public static void main(String[] args)
 {
//  checkQQ2(args[0]);

  String QQ = "123";
  String qqreg = "[1-9]\\d{4,14}";
  boolean b = QQ.matches(qqreg);
//  System.out.println("qq="+b);


  String tel = "14800001234";

  String telreg = "1[35]\\d{9}";

  boolean b1 = tel.matches(telreg);

//  System.out.println("tel:"+b1);

  String str = "da jia ming tian bu fang jia !";
  
  String strreg = "\\b[a-z]{3}\\b";
//  strreg = "\\b[a-z]*a[a-z]*\\b";
  //1.将规则封装成对象。
  Pattern p = Pattern.compile(strreg);

  //2,要将规则作用到字符串上。因为符合规则的元素不唯一,
  //所以将这些元素封装到了Matcher对象中。通过Matcher对象方法可以获取需要的数据。
  Matcher m = p.matcher(str);
//  while(m.find())
//   System.out.println(m.group());

 


  //切割:split();
  String s = "sadfzzasqqqfsttttdf";

  String[] arr = s.split("(.)\\1+");

  for(String ss : arr)
  {
//   System.out.println(ss);
  }


  //替换 String replaceAll(regex,str);
  
  String sreg = "(.)\\1+";

  s = s.replaceAll("(.)\\1+","$1");
//  System.out.println("s="+s);

//  test_2();
  checkMail();
 }

 public static void test_1()
 {
  String s = "我我.....我我我...我我.....要要要要....要要学...学学....学学...学编编编编...编编编...程程程....程";

  s = s.replaceAll("\\.+","");

  s = s.replaceAll("(.)\\1+","$1");
  System.out.println("s="+s);
 }
 

 public static void test_2()
 {
  String ip = "192.168.1.254 10.10.10.10 2.2.2.2  66.105.88.10";

  ip = ip.replaceAll("(\\d+)","00$1");

  ip = ip.replaceAll("0*(\\d{3})","$1");
  System.out.println(ip);
  
  String[] arr = ip.split(" +");

  TreeSet<String> ts = new TreeSet<String>();
  for(String s : arr)
  {
   ts.add(s);
   
  }
  for(String s : ts)
  {
   System.out.println(s.replaceAll("0*(\\d+)","$1"));
  }


 }
 
 public static void checkMail()
 {
  String mail = "abc1@sina.com.cn";
  mail = "1@1.1";

  String mailreg= "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+";
  
  mailreg = "\\w+@\\w+(\\.\\w+)+";


  boolean ismail = mail.matches(mailreg);

  System.out.println("mail:"+ismail);
 }
 
 /*
 要求:
 5~15,必须是数字,而且0不可以开头。
 */
 public static void checkQQ(String qq)
 {
  int len = qq.length();

  if(len>=5 && len <=15)
  {
   if(qq.startsWith("0"))
    System.out.println("0不可以开头");
   else
   {
    boolean b = true;

    char[] arr = qq.toCharArray();
    for(int x=0; x<arr.length; x++)
    {
     char ch = qq.charAt(x);
     if(!(ch>='0' && ch<='9'))
     {
      b = false;
      break;
     }
    }
    if(b)
    {
     System.out.println("qq:"+qq);
    }
    else
    {
     System.out.println("出现非法字符");
    }
   }
  }
  else
  {
   System.out.println("长度错误");
  }
 }

 public static void checkQQ2(String qq)
 {
  int len = qq.length();

  if(len>=5 && len <=15)
  {
   if(qq.startsWith("0"))
    System.out.println("0不可以开头");
   else
   {
    try
    {
     Long l = Long.parseLong(qq);
     System.out.println("qq:"+l);
    }
    catch (NumberFormatException e)
    {
     System.out.println("非法");
    }
    
   }
  }
  else
  {
   System.out.println("长度错误");
  }
 }
}

原创粉丝点击