黑馬程序員-正則表達(dá)式

-------android培訓(xùn)java培訓(xùn)期待與您交流!----------

概述:
  • 符合一定規(guī)則的表達(dá)式,用于專(zhuān)門(mén)操作字符串。
  • 簡(jiǎn)化字符串書(shū)寫(xiě)和操作。
  • 符號(hào)定義越多,正則越長(zhǎng),閱讀性越差。
package com.sergio.Regex;

/**
 * 正則表達(dá)式實(shí)例,校驗(yàn)qq數(shù)字。
 * Created by Sergio on 2015-05-24.
 */
public class RegexDemo {
    public static void main(String[] args) {
        checkQQ();
    }

    public static void checkQQ() {
        String qq = "123845";
        //校驗(yàn)qq數(shù)字,5-15之間位數(shù),不能從0開(kāi)始,還只能是數(shù)字
        String regex = "[1-9][0-9]{4,14}";
        boolean flag = qq.matches(regex);
        if (flag) {
            System.out.println(qq + "格式正確");
        } else {
            System.out.println(qq + "格式正確");
        }
    }
}
匹配
  • String的matches()方法可以用來(lái)匹配整串字符串,只要有一處不符合規(guī)則,就匹配結(jié)束返回false。
  • 匹配常見(jiàn)規(guī)則有:
regex.png
package com.sergio.Regex;

/**
 * 匹配13xxx,15xxx,18xxx開(kāi)頭的手機(jī)號(hào)碼
 * Created by Sergio on 2015-05-24.
 */
public class TelCheckDemo {
    public static void main(String[] args) {
        String tel = "18992763318";
        String reg = "1[358]\\d{9}";
        System.out.println(tel.matches(reg));
    }
}
切割
  • String的split()方法用來(lái)切割字符串。
package com.sergio.Regex;

/**
 * 切割
 * Created by Sergio on 2015-05-25.
 */
public class SplitDemo {
    public static void main(String[] args) {
        //用點(diǎn)切割
        splitString("zhangsan.lisi.wangwu", "\\,");
        //文件路徑切割
        splitString("c:\\abc\\a.txt", "\\\\");
        //按照疊詞切割,()代表一個(gè)組,\1代表使用第一位的結(jié)果,+代表多維
        splitString("abccsfkksdfqquyiaaaau", "(.)\\1+");
    }

    public static void splitString(String str, String reg) {
        String[] arr = str.split(reg);
        for (String s : arr) {
            System.out.println(s);
        }
    }
}
替換
  • String的replaceAll()方法來(lái)做字符串替換。
package com.sergio.Regex;

/**
 * 切割
 * Created by Sergio on 2015-05-25.
 */
public class SplitDemo {
    public static void main(String[] args) {
        //用點(diǎn)切割
        splitString("zhangsan.lisi.wangwu", "\\,");
        //文件路徑切割
        splitString("c:\\abc\\a.txt", "\\\\");
        //按照疊詞切割,()代表一個(gè)組,\1代表使用第一位的結(jié)果,+代表多維
        splitString("abccsfkksdfqquyiaaaau", "(.)\\1+");
    }

    public static void splitString(String str, String reg) {
        String[] arr = str.split(reg);
        for (String s : arr) {
            System.out.println(s);
        }
    }
}
獲取
  • 將字符串中的符合規(guī)則的字串取出。操作步驟:
    1. 將正則表達(dá)式封裝成對(duì)象。
    2. 讓正則對(duì)象和要操作的字符串相關(guān)聯(lián)。
    3. 關(guān)聯(lián)后,獲取正則匹配引擎。
    4. 通過(guò)引擎對(duì)符合規(guī)則的字串進(jìn)行操作,比如取出。
package com.sergio.Regex;

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

/**
 * 正則表達(dá)式獲取的功能步驟
 * Created by Sergio on 2015-05-25.
 */
public class GetDemo {
    public static void main(String[] args) {
        getDemo();
    }

    public static void getDemo() {
        String str = "ming tian jiu kai shi fang jia le !";
        //獲取三個(gè)字母
        String reg = "\\b[a-z]{3}\\b";
        //將規(guī)則封裝成對(duì)象
        Pattern p = Pattern.compile(reg);
        //讓正則對(duì)象和作用的字符串關(guān)聯(lián),獲取匹配器對(duì)象
        Matcher m = p.matcher(str);

        //一下的兩部動(dòng)作是迭代查找匹配的,可以使用循環(huán)替代
        //將規(guī)則作用到字符串上,并進(jìn)行符合規(guī)則的字串查找,
        //boolean b = m.find();
        //用于獲取匹配后的結(jié)果
        //System.out.println(m.group());
        while (m.find()) {
            System.out.println(m.group());
            System.out.println(m.start() + "::" + m.end());
        }
    }
}
  • 練習(xí)。郵件地址匹配檢測(cè)。
package com.sergio.Regex;

import java.util.TreeSet;

/**
 * 將重復(fù)詞的句子編程我要學(xué)編程
 * Created by Sergio on 2015-05-25.
 */
public class Demo1 {
    public static void main(String[] args) {
        checkMail();
    }
    //對(duì)郵件地址進(jìn)行校驗(yàn)
    public static void checkMail() {
        String mail = "123wer32453@sina.com.cn";
        //郵件地址的名字必須是6-12位,后綴匹配為.com或者.cn
        String regex = "[a-zA-Z0-9_]{6,12}@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+";//較為精確的匹配
        //regex = "\\w+@\\W+(\\.\\w+)";模糊匹配
        System.out.println(mail.matches(regex));
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容