正則表達式的規則及應用

第三階段 JAVA常見對象的學習

正則表達式

(一) 正則表達式概述

(1) 簡單概述

就是符合必定規則的字符串、java

(2) 常見規則

A:字符

x 字符 x。舉例:'a'表示字符a
\ 反斜線字符。
\n 新行(換行)符 ('\u000A') 
\r 回車符 ('\u000D')

			
B:字符類

[abc] a、b 或 c(簡單類) 
[^abc] 任何字符,除了 a、b 或 c(否認) 
[a-zA-Z] a到z 或 A到Z,兩頭的字母包括在內(範圍) 
[0-9] 0到9的字符都包括

			
C:預約義字符類

. 任何字符。個人就是.字符自己,怎麼表示呢 \.
\d 數字:[0-9]
\w 單詞字符:[a-zA-Z_0-9]
在正則表達式裏面組成單詞的東西必須有這些東西組成

 
D:邊界匹配器

^ 行的開頭 
$ 行的結尾 
\b 單詞邊界
就是否是單詞字符的地方。
舉例:hello world?haha;xixi

			

E:Greedy 數量詞 

X? X,一次或一次也沒有
X* X,零次或屢次
X+ X,一次或屢次
X{n} X,剛好 n 次 
X{n,} X,至少 n 次 
X{n,m} X,至少 n 次,可是不超過 m 次

(3) 常見功能

//判斷功能
String類的public boolean matches(String regex)
    
//分割功能
String類的public String[] split(String regex)
    
//替換功能
String類的public String replaceAll(String regex,String replacement)
    
//獲取功能
Pattern和Matcher
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
				
find():查找存不存在
group():獲取剛纔查找過的數據

正則表達式是很是強大的,咱們經過幾個簡單的例子來看一下正則表達式的應用正則表達式

(二) 正則表達式的應用

(1) 判斷功能以及正則表達式——驗證郵箱格式案例

import java.util.Scanner;

public class RegexDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入你的郵箱");
        String email = sc.nextLine();

        //定義郵箱規則
        //"[a-zA-Z_0-9]+@[a-zA-Z_0-9]{2,6}(\\.[a-zA-Z_0-9]{2,3})+"
        //簡化後
        String regex = "\\w+@\\w{2,6}(\\.\\w{2,3}+)";
        boolean flag = email.matches(regex);
        System.out.println(flag);
    }
}

(2) 拆分功能——好友年齡範圍限制

import java.util.Scanner;

/*
 *  分割功能
 *          String類的 public String[] split(String regex)
 *          根據正則表達式的匹配拆分此字符串
 *  舉例:
 *          社交軟件中
 *          搜索好友:
 *              性別:女
 *              年齡:18-24
 */
public class RegexDemo {
    public static void main(String[] args) {
        String ages = "18-24";

        //定義規則
        String regex = "-";

        //調用方法
        String[] strArray = ages.split(regex);

        //獲得int類型
        int StartAge = Integer.parseInt(strArray[0]);
        int EndAge = Integer.parseInt(strArray[1]);

        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入你的年齡");
        int age = sc.nextInt();
        if (age >= StartAge && age <= EndAge){
            System.out.println("確認過眼神,我趕上對的人!");
        }else{
            System.out.println("惋惜不是你,陪我到最後!");
        }
    }
}

(3) 把字符串中的數字排序

import java.util.Arrays;

public class RegexDemo2 {
    public static void main(String[] args) {
        String s = "22 33 55 88 66 11";
        String[] strArray = s.split(" ");

        //把字符串數字轉換成int數組
        int[] arr = new int[strArray.length];
        for (int x = 0; x < arr.length; x++) {
            arr[x] = Integer.parseInt(strArray[x]);
        }
        Arrays.sort(arr);

        //把排序後的int數組再組裝成一個字符串
        StringBuilder sb = new StringBuilder();
        for (int x = 0; x < arr.length; x++) {
            sb.append(arr[x]).append(" ");
        }

        String result = sb.toString().trim();
        System.out.println("result: " + result);
    }
}

//運行結果
result: 11 22 33 55 66 88

(4) 替換功能

package cn.bwh_03_RegexReplaceAll;

public class RegexDemo {
    public static void main(String[] args) {
        String s1 = "hello123456world";

        //全部數字用*給替換
        String regex = "\\d";
        String s2 = "*";

        String result = s1.replaceAll(regex, s2);
        System.out.println(result);
    }
}

//運行結果
hello******world

(5) 獲取字符串中由3個字符組成的單詞

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexDemo {
    public static void main(String[] args) {
        String s = "hao hao xue xi tian tian xiang shang";

        //規則
        String regex = "\\b\\w{3}\\b";

        //把規則編譯成模式對象
        Pattern p = Pattern.compile(regex);
        //經過模式對象獲得匹配器對象
        Matcher m = p.matcher(s);

        while (m.find()){
            System.out.println(m.group());
        }
    }
}

//運行結果
hao
hao
xue

結尾:

若是內容中有什麼不足,或者錯誤的地方,歡迎你們給我留言提出意見, 蟹蟹你們 !^_^數組

若是能幫到你的話,那就來關注我吧!(系列文章均會在公衆號第一時間更新)app

在這裏的咱們素不相識,卻都在爲了本身的夢而努力 ❤學習

一個堅持推送原創Java技術的公衆號:理想二旬不止ui

img

相關文章
相關標籤/搜索