Java笔记(十)

正则表达式:

符合一定规则的表达式,用于专门操作字符串。

对QQ号码进行校验,要求:5-11位,0不能开头,只能是数字。

 1 public class Demo{
 2     public static void main(String[] args) {
 3         String qq = "741603187";
 4         String regex = "[1-9][0-9]{4,10}";
 5         boolean flag = qq.matches(regex);
 6         if(flag){
 7             System.out.println("OK");
 8         }
 9         else{
10             System.out.println("X");
11         }
12     }
13 }

操作功能:

(1)匹配:String  matches()方法

  [abc]:a,b或c

  [^abc]:任何字符,除了a,b或c

  [a-zA-Z]:a到z或A到Z,包含两头

  .   任何字符

  d   数字[0-9]

  D   非数字[^0-9]

  w 单词字符[a-zA-Z_0-9]数字、字母、下划线

  W  非单词字符

  X?    X,一次或一次也没有

  X*    X,零次或多次

  X+    X,一次或多次

    X{n}   X,恰好n次

  X{n,}   X,至少n次

    X{n,m}   X,至少n次,但是不超过m次

(2)切割:String  split()方法

例:按照叠词完成切割:

为了可以让规则的结果被重用,可以将规则封装成一个组,用( )完成。组的出现都有编号,从1开始。想要使用已有的组可以通过 (n就是组的编号)的形式来获取。

 1 public class Demo{
 2     public static void main(String[] args) {
 3         String s = "abcccdeffghhhhhi";
 4         String regex = "(.)\1+";
 5         String[] arr = s.split(regex);
 6         for(String str : arr){
 7             System.out.println(str);
 8         }
 9     }
10 }

输出结果:

ab
de
g
i

(3)替换:String   replaceAll()方法

 1 public class Demo{
 2     public static void main(String[] args) {
 3         //将字符串中的数字替换成*
 4         String s1 = "abcc12432ffg333hi42314";
 5         String regex1 = "\d{4,}";
 6         myReplace(s1, regex1, "*");
 7         
 8         //将字符串中重叠的字符替换成单个字母,如aaa->a
 9         String s2 = "abcccdeefffffg";
10         String regex2 = "(.)\1+";
11         myReplace(s2, regex2, "$1"); //$n,获取第n组中的内容
12         
13     }
14     public static void myReplace(String str, String regex, String newStr){
15         str = str.replaceAll(regex, newStr);
16         System.out.println(str);
17     }
18 }

输出结果:

abcc*ffg333hi*
abcdefg

(4)获取:将字符串中符合规则的子串取出

步骤:将正则表达式封装成对象

   让正则表达式和要操作的字符串相关联

   关联后,获取正则匹配的引擎

   通过引擎对符合规则的子串进行操作,比如取出

 1 import java.util.regex.Matcher;
 2 import java.util.regex.Pattern;
 3 
 4 public class Demo{
 5     public static void main(String[] args) {
 6         String str = "jin tian xing qi ji";
 7         String regex = "\b[a-z]{4}\b"; // 单词边界
 8         
 9         //将规则封装成对象
10         Pattern p = Pattern.compile(regex);
11         
12         //让正则对象和要作用的字符串相关联。获取匹配器对象
13         Matcher m = p.matcher(str);
14         
15         //讲规则作用在字符串上,并进行符合规则的子串查找
16         while(m.find()){
17             System.out.println(m.group());
18         }
19     }
20 }

输出结果:

tian
xing

例:

将下列字符串转成:大家早上好

将已有字符串变成另一个字符串,使用替换功能,可以先将 . 去掉,再将多个重复的内容变成单个内容

 1 public class Demo{
 2     public static void main(String[] args) {
 3         test();
 4     }
 5     public static void test(){
 6         String s = "大大大...家家家家家家家..早早早...上上上上上上上..好好";
 7         s = s.replaceAll("\.+", "");
 8         s = s.replaceAll("(.)\1+", "$1");
 9         System.out.println(s);
10     }
11 }

将ip地址进行地址段顺序排序:

按照字符串自然顺序,只要让它们每一段都是3位即可。

(1) 按照每一段需要的最多的0进行补齐,那么每一段就会至少保证有3位

(2) 将每一段只保留3位。这样。所有的ip地址都是每一段3位

 1 import java.util.TreeSet;
 2 
 3 //import java.util.Arrays;
 4 
 5 public class Demo{
 6     public static void main(String[] args) {
 7         test();
 8     }
 9     public static void test(){
10         String ip = "192.68.1.254 102.49.23.13 10.10.10.10 2.2.2.2 8.109.90.30";
11         //取一连串连续数字,并在数字前加00
12         ip = ip.replaceAll("(\d+)", "00$1"); 
13         
14         //将以0开头(没有或多个)的,并3个数字结尾的一连串数字,替换成后三个数字。如002->002, 0010->010, 00192->192
15         ip = ip.replaceAll("0*(\d{3})", "$1");  
16         
17         String[] arr = ip.split(" ");
18         //Arrays.sort(arr);
19         TreeSet<String> ts = new TreeSet<String>();
20         for(String s : arr){
21             ts.add(s);
22         }
23         for(String s : ts){
24             System.out.println(s.replaceAll("0*(\d+)", "$1"));
25         }
26     }
27 }

输出结果:

2.2.2.2
8.109.90.30
10.10.10.10
102.49.23.13
192.68.1.254

对邮件地址进行校验:

 1 public class Demo{
 2     public static void main(String[] args) {
 3         test();
 4     }
 5     public static void test(){
 6         String mail = "abc12@sina.com.cn";
 7         String regex = "\w+@[a-zA-Z0-9]+(\.[a-zA-Z]+){1,3}";
 8         System.out.println(mail.matches(regex));
 9     }
10 }
原文地址:https://www.cnblogs.com/zhangtianq/p/6364501.html