java正则表达式

1.为什么要用正则表达式

   如果不用正则表达式判断某一个字符串是否满足格式要求显然会有点麻烦,比如我们对qq进行判断,如果采用如下代码:

 1 import java.util.*;
 2 import java.io.*;
 3 
 4 
 5 public class String_init {
 6 
 7     public static void main(String[] args) throws FileNotFoundException{
 8         String str = "123456789";
 9         char []ch = str.toCharArray();//转换为char数组
10         boolean isQQ = true;
11         for(char chs:ch)
12         {
13             if(chs <'0' || chs > '9')
14             {
15                 isQQ = false;
16                 break;
17             }
18         }
19         
20         if(isQQ)
21         {
22             System.out.println("是QQ");
23         }
24         else
25         {
26             System.out.println("不是QQ");
27         }
28     }
29 }

是不是觉得很麻烦?

  如果我们采用正则表达式来进行判断,那么代码如下:

 1 import java.util.*;
 2 import java.io.*;
 3 
 4 
 5 public class String_init {
 6 
 7     public static void main(String[] args) throws FileNotFoundException{
 8         String str = "123456789";
 9         if(str.matches("\d+"))
10         {
11             System.out.println("isQQ");
12         }
13         else
14         {
15             System.out.println("notQQ");
16         }
17     }
18 }

  是不是更简单快捷.所以采用正则表达式还是非常有必要的.代码中d代表数字,+代表1次或者多次,之所以用两个\是因为考虑到了转义字符.

  另外一个小案例,提取字符串中的三个字母组成的单词

 1 import java.util.regex.*;
 2 
 3 
 4 public class String_init {
 5 
 6     public static void main(String[] args){
 7         String str="da jia hao,ming tian bu fang jia";
 8         // 表示单词开始和结束
 9         String regex = "\b[a-zA-Z]{3}\b";
10         //正则表达式的逻辑
11         Pattern p = Pattern.compile(regex);
12         Matcher m = p.matcher(str);//挖掘所有符合条件的子串
13         while(m.find())
14         {
15             System.out.println(m.group());//输出匹配的子字符串
16             System.out.println(m.start()+":"+m.end());//输出这个字符串的起始位置
17         }
18     }
19 }

2.利用正则表达式的几个小程序

  用正则表达式替换字符串中的满足条件的某个字符串:

 1 import java.util.regex.*;
 2 
 3 
 4 public class String_init {
 5 
 6     public static void main(String[] args){
 7         String str = "hello1234wwwwwwwwww5678tttttttttttttxianxia";
 8         
 9         //替换
10         //str = str.replaceAll("[0-9]{4}", "ABCD");
11         str = str.replaceAll("[a-zA-Z]{5,}", "!");//连续超过五次的字符替换
12         System.out.println(str);
13         
14     }
15 }

  split拆分

 1 import java.util.regex.*;
 2 
 3 
 4 public class String_init {
 5 
 6     public static void main(String[] args){
 7         String str = "hellowwwwwwwwwwaaaaaaadafengchuioooooooooooooofeixiang";
 8         String []names = str.split("(.)\1+");//(.)代表任意字符  1+:1个字符连续出现多次
 9         for(String st:names)
10         {
11             System.out.println(st);
12         }
13     }
14 }

  正则表达式匹配验证与小结

  判断是否是QQ号

 1 import java.util.regex.*;
 2 
 3 
 4 public class String_init {
 5 
 6     public static void main(String[] args){
 7         String str = "1231";
 8         String regex="\d{5,10}";//5-10位数字
 9         boolean isit = str.matches(regex);
10         System.out.println(isit);
11     }
12 }
原文地址:https://www.cnblogs.com/xiaochi/p/9178722.html