最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

前言

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

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

  • matches()方法:匹配整個區(qū)域。
  • find()方法:嘗試查找與該模式匹配的輸入序列的下一個子序列,簡單來說就是匹配符合的目標就會返回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
    }

只有當字符串全是數(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()方法匹配結果基本一致,你可能會發(fā)現(xiàn)這種表達式與上面[0-9]+一樣的結果,但是一定要注意,你所需要的業(yè)務需求是什么,準確的表達式更容易理解。

這種表達式很簡單的告訴我們只要包含數(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()方法匹配結果基本一致,包含字母返回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
    }

字符串只有純特殊字符才會返回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。

正則表達式

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

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

*+ 限定符都是貪婪的,因為它們會盡可能多的匹配文字。

總結

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

相關文章

最新評論

桑日县| 万安县| 万州区| 天等县| 明光市| 大关县| 林芝县| 莆田市| 河曲县| 白河县| 万荣县| 微博| 罗甸县| 镇沅| 临朐县| 石楼县| 大安市| 东乡族自治县| 洛宁县| 萝北县| 乌兰浩特市| 上饶县| 偏关县| 河间市| 西城区| 隆德县| 竹北市| 阳春市| 化州市| 建湖县| 青岛市| 平和县| 鄂尔多斯市| 广水市| 福鼎市| 儋州市| 平山县| 扶余县| 临夏县| 仁化县| 读书|