欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java正則表達(dá)式判斷是否包含數(shù)字、字母、特殊字符及中文的多種方法

 更新時(shí)間:2023年08月01日 11:38:32   作者:成為大佬先禿頭  
這篇文章主要給大家介紹了關(guān)于Java正則表達(dá)式判斷是否包含數(shù)字、字母、特殊字符及中文的多種方法,Java正則表達(dá)式在字符串處理和模式匹配中扮演著重要角色,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

業(yè)務(wù)需要,判斷中文字符串中是否有其他字符,你可以使用表達(dá)式:數(shù)字、大小寫字母、特殊符號來判斷是不是純中文,但是最簡單的還是使用中文匹配規(guī)則判斷。

Matcher類的matches()和find()的區(qū)別

  • matches()方法:匹配整個(gè)區(qū)域。
  • find()方法:嘗試查找與該模式匹配的輸入序列的下一個(gè)子序列,簡單來說就是匹配符合的目標(biāo)就會(huì)返回true。

方法一:數(shù)字匹配

純數(shù)字匹配

Pattern compile = Pattern.compile("[0-9]+");
  • matches()方法
    public static void main(String[] args) {
        String str = "qwer2";
        Pattern compile = Pattern.compile("[0-9]+");
        Matcher matcher = compile.matcher(str);
        boolean b = matcher.matches();
        System.out.println(b);//Output: false
        str = "123";
        matcher = compile.matcher(str);
        b = matcher.matches();
        System.out.println(b);//Output: true
    }

只有當(dāng)字符串全是數(shù)字時(shí),返回true。

  • find()方法
    public static void main(String[] args) {
        String str = "2qw2er2";
        Pattern compile = Pattern.compile("[0-9]+");
        Matcher matcher = compile.matcher(str);
        boolean b = matcher.find();
        System.out.println(b);//Output: true
        str = "qwer";
        matcher = compile.matcher(str);
        b = matcher.find();
        System.out.println(b);//Output: false
    }

只要字符串中包含數(shù)字返回true,除非字符串沒有數(shù)字,則返回false

  • 包含數(shù)字匹配
Pattern compile = Pattern.compile(".*[0-9].*");

matches()方法

    public static void main(String[] args) {
        String str = "qwer2";
        Pattern compile = Pattern.compile(".*[0-9].*");
        Matcher matcher = compile.matcher(str);
        boolean b = matcher.matches();
        System.out.println(b);//Output: true
        str = "qwer";
        matcher = compile.matcher(str);
        b = matcher.matches();
        System.out.println(b);//Output: false
    }

find()方法匹配結(jié)果基本一致,你可能會(huì)發(fā)現(xiàn)這種表達(dá)式與上面[0-9]+一樣的結(jié)果,但是一定要注意,你所需要的業(yè)務(wù)需求是什么,準(zhǔn)確的表達(dá)式更容易理解。

這種表達(dá)式很簡單的告訴我們只要包含數(shù)字返回true,除非字符串沒有數(shù)字,則返回false

方法二:字母匹配

純字母匹配

Pattern compile = Pattern.compile("[a-zA-Z]+");
  • matches()方法
        String str = "Qwer2";
        Pattern compile = Pattern.compile("[a-zA-Z]+");
        Matcher matcher = compile.matcher(str);
        boolean b = matcher.matches();
        System.out.println(b);//Output: false
        str = "Qwer";
        matcher = compile.matcher(str);
        b = matcher.matches();
        System.out.println(b);//Output: true

無論字母大小寫,只要字符串中全是字母,返回true,否則為false

  • find()方法
    public static void main(String[] args) {
        String str = "Qwer2";
        Pattern compile = Pattern.compile("[a-zA-Z]+");
        Matcher matcher = compile.matcher(str);
        boolean b = matcher.find();
        System.out.println(b);//Output: true
        str = "Qwer";
        matcher = compile.matcher(str);
        b = matcher.find();
        System.out.println(b);//Output: true
    }

無論字母大小寫,只要字符串包含字母,返回true,否則返回false

包含字母匹配

Pattern compile = Pattern.compile(".*[a-zA-z].*");
  • matches()方法
    public static void main(String[] args) {
        String str = "Qwer2";
        Pattern compile = Pattern.compile(".*[a-zA-z].*");
        Matcher matcher = compile.matcher(str);
        boolean b = matcher.matches();
        System.out.println(b);//Output: true
        str = "1234";
        matcher = compile.matcher(str);
        b = matcher.matches();
        System.out.println(b);//Output: false
    }

find()方法與matches()方法匹配結(jié)果基本一致,包含字母返回true。

方法三:特殊字符匹配

純特殊字符匹配

Pattern compile = Pattern.compile("[[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“'。,、?]|\\n|\\r|\\t]+");
  • matches()方法
    public static void main(String[] args) {
        String str = "~!@#$`";
        Pattern compile = Pattern.compile("[[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“'。,、?]|\\n|\\r|\\t]+");
        Matcher matcher = compile.matcher(str);
        boolean b = matcher.matches();
        System.out.println(b);//Output: true
        str = "~!@#$`2";
        matcher = compile.matcher(str);
        b = matcher.matches();
        System.out.println(b);//Output: false
    }

字符串只有純特殊字符才會(huì)返回true,如果包含字母或數(shù)字、等,返回false

前面多次介紹find()方法,后續(xù)不做過多介紹。

包含特殊字符匹配

Pattern compile = Pattern.compile(".*[[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“'。,、?]|\\n|\\r|\\t].*");
  • matches()方法
    public static void main(String[] args) {
        String str = "~!@#$`2";
        Pattern compile = Pattern.compile(".*[[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“'。,、?]|\\n|\\r|\\t].*");
        Matcher matcher = compile.matcher(str);
        boolean b = matcher.matches();
        System.out.println(b);//Output: true
        str = "qwer2";
        matcher = compile.matcher(str);
        b = matcher.matches();
        System.out.println(b);//Output: false
    }

字符串包含特殊字符返回true,否則返回false。

方法四:數(shù)字+字母匹配

純數(shù)字+字母匹配

Pattern compile = Pattern.compile("[0-9a-zA-z]+");
  • matches()方法
    public static void main(String[] args) {
        String str = "Qwer2";
        Pattern compile = Pattern.compile("[0-9a-zA-z]+");
        Matcher matcher = compile.matcher(str);
        boolean b = matcher.matches();
        System.out.println(b);//Output: true
        str = "Qwer2~";
        matcher = compile.matcher(str);
        b = matcher.matches();
        System.out.println(b);//Output: false
    }

字符串為數(shù)字或大小寫字母返回true,否則返回false。

包含數(shù)字+字母匹配

Pattern compile = Pattern.compile(".*[0-9a-zA-z].*");
  • matches()方法
    public static void main(String[] args) {
        String str = "Qwer2%";
        Pattern compile = Pattern.compile(".*[0-9a-zA-z].*");
        Matcher matcher = compile.matcher(str);
        boolean b = matcher.matches();
        System.out.println(b);//Output: true
        str = "~@#";
        matcher = compile.matcher(str);
        b = matcher.matches();
        System.out.println(b);//Output: false
    }

字符串包含數(shù)字或大小寫字母返回true,否則返回false。

方法五:中文匹配

純中文匹配

Pattern compile = Pattern.compile("[\\u4e00-\\u9fa5]+");
  • matches()方法
    public static void main(String[] args) {
        String str = "中文";
        Pattern compile = Pattern.compile("[\\u4e00-\\u9fa5]+");
        Matcher matcher = compile.matcher(str);
        boolean b = matcher.matches();
        System.out.println(b);//Output: true
        str = "中文2";
        matcher = compile.matcher(str);
        b = matcher.matches();
        System.out.println(b);//Output: false
    }

字符串為純中文返回true,否則返回false

包含中文匹配

Pattern compile = Pattern.compile(".*[\\u4e00-\\u9fa5].*");
  • matches()方法
    public static void main(String[] args) {
        String str = "中文2";
        Pattern compile = Pattern.compile(".*[\\u4e00-\\u9fa5].*");
        Matcher matcher = compile.matcher(str);
        boolean b = matcher.matches();
        System.out.println(b);//Output: true
        str = "qwer2";
        matcher = compile.matcher(str);
        b = matcher.matches();
        System.out.println(b);//Output: false
    }

字符串包含中文返回true,否則返回false。

正則表達(dá)式

通過上面的介紹,你可能會(huì)發(fā)現(xiàn)一個(gè)規(guī)律,[匹配規(guī)則]+表示完整匹配、.*[匹配規(guī)則].*表示包含匹配,只要替換里面規(guī)則就能實(shí)現(xiàn)對應(yīng)的操作,但是并不理解它們的含義,下面介紹正則表達(dá)式的一些語句以及含義。

  • [ABC]:匹配 [...] 中的所有字符。
  • [^ABC]:匹配除了 [...] 中字符的所有字符。
  • [A-Z]:表示一個(gè)區(qū)間,匹配所有大寫字母,[a-z] 表示所有小寫字母。
  • *:匹配前面的子表達(dá)式零次或多次。例如,zo* 能匹配 “z” 以及 “zoo”。
  • +:匹配前面的子表達(dá)式一次或多次。例如,zo+ 能匹配 “zo” 以及 “zoo”,但不能匹配 “z”。
  • .:匹配除換行符 \n 之外的任何單字符。
  • ?:匹配前面的子表達(dá)式零次或一次。

*+ 限定符都是貪婪的,因?yàn)樗鼈儠?huì)盡可能多的匹配文字。

總結(jié)

到此這篇關(guān)于Java正則表達(dá)式判斷是否包含數(shù)字、字母、特殊字符及中文的多種方法的文章就介紹到這了,更多相關(guān)Java正則判斷包含其他字符內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring?@Bean注解深入分析源碼執(zhí)行過程

    Spring?@Bean注解深入分析源碼執(zhí)行過程

    隨著SpringBoot的流行,我們現(xiàn)在更多采用基于注解式的配置從而替換掉了基于XML的配置,所以本篇文章我們主要探討基于注解的@Bean以及和其他注解的使用
    2023-01-01
  • SpringMvc響應(yīng)數(shù)據(jù)及結(jié)果視圖實(shí)現(xiàn)代碼

    SpringMvc響應(yīng)數(shù)據(jù)及結(jié)果視圖實(shí)現(xiàn)代碼

    這篇文章主要介紹了SpringMvc響應(yīng)數(shù)據(jù)及結(jié)果視圖實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • springboot @JsonSerialize的使用講解

    springboot @JsonSerialize的使用講解

    這篇文章主要介紹了springboot @JsonSerialize的使用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java線程安全狀態(tài)專題解析

    Java線程安全狀態(tài)專題解析

    線程安全是多線程編程時(shí)的計(jì)算機(jī)程序代碼中的一個(gè)概念。在擁有共享數(shù)據(jù)的多條線程并行執(zhí)行的程序中,線程安全的代碼會(huì)通過同步機(jī)制保證各個(gè)線程都可以正常且正確的執(zhí)行,不會(huì)出現(xiàn)數(shù)據(jù)污染等意外情況
    2022-03-03
  • 簡單的java圖片處理類(圖片水印 圖片縮放)

    簡單的java圖片處理類(圖片水印 圖片縮放)

    本圖片處理類功能非常之強(qiáng)大可以實(shí)現(xiàn)幾乎所有WEB開發(fā)中對圖像的處理功能都集成了,包括有縮放圖像、切割圖像、圖像類型轉(zhuǎn)換、彩色轉(zhuǎn)黑白、文字水印、圖片水印等功能
    2013-11-11
  • SpringData JPA基本/高級/多數(shù)據(jù)源的使用詳解

    SpringData JPA基本/高級/多數(shù)據(jù)源的使用詳解

    這篇文章主要介紹了SpringData JPA基本/高級/多數(shù)據(jù)源的使用詳解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 詳解阿里云maven鏡像庫配置(gradle,maven)

    詳解阿里云maven鏡像庫配置(gradle,maven)

    這篇文章主要介紹了詳解阿里云maven鏡像庫配置(gradle,maven),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-02-02
  • 深入剖析理解AsyncGetCallTrace源碼底層原理

    深入剖析理解AsyncGetCallTrace源碼底層原理

    這篇文章主要為大家介紹了AsyncGetCallTrace源碼的深層原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-02-02
  • 為IntelliJ IDEA配置JVM參數(shù)的兩種方法

    為IntelliJ IDEA配置JVM參數(shù)的兩種方法

    在使用IntelliJ IDEA進(jìn)行Java開發(fā)時(shí),合理配置JVM參數(shù)對于優(yōu)化項(xiàng)目性能和資源管理至關(guān)重要,IntelliJ IDEA提供了兩種方便的方式來設(shè)置JVM參數(shù),本文將詳細(xì)介紹這兩種方法:通過工具欄編輯配置和通過服務(wù)編輯配置,需要的朋友可以參考下
    2024-12-12
  • Mybatis核心配置文件加載流程詳解

    Mybatis核心配置文件加載流程詳解

    本文將介紹MyBatis在配置文件加載的過程中,如何加載核心配置文件、如何解析映射文件中的SQL語句以及每條SQL語句如何與映射接口的方法進(jìn)行關(guān)聯(lián),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12

最新評論