专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 正则表达式任意字符的匹配

正则表达式任意字符的匹配

更新时间:2021-08-12 12:24:25 来源:动力节点 浏览4539次

在regex 中,我们可以使用句点"."字符匹配任何字符。为了只匹配一组给定的字符,我们应该使用字符类

1. 使用正则匹配任意字符

'.'字符将匹配任何字符,而不管它是什么字符。匹配的字符可以是字母,数字的任何特殊字符。

默认情况下,句点/点字符仅匹配单个字符。为了创建更有意义的模式,我们可以将它与其他正则表达式结构结合起来。

图案 描述
“。” 仅匹配单个字符。
“AB” 匹配以 'A' 开头并以 'B' 结尾的 3 个字符长字符串中位于第二位的任何字符。
“.*” 匹配任意数量的字符。
import java.util.regex.Pattern;
public class Main {
    public static void main(String[] args) 
    {
        System.out.println(Pattern.compile(".").matcher("a").matches());    //true
        System.out.println(Pattern.compile(".").matcher("ab").matches());   //false
 
        System.out.println(Pattern.compile("A.B").matcher("AIB").matches());    //true
        System.out.println(Pattern.compile("A.B").matcher("ABI").matches());    //false
         
        System.out.println(Pattern.compile(".*").matcher("AIB").matches());     //true
    }
}

程序输出。

true
false
true
false
true

2.使用正则表达式匹配固定字符集

如果我们想在任何地方匹配一组字符,我们需要使用字符类。例如,'[abc]' 将匹配单个字符,它可以是 'a'、'b' 或 'c'。

图案 描述
“[ABC]” 仅匹配给定字符集中的单个字符。
“[aA]” 匹配字符“a”,不区分大小写。
import java.util.regex.Pattern; 
public class Main 
{
    public static void main(String[] args) 
    {
        System.out.println(Pattern.compile("[abc]").matcher("a").matches());    //true
        System.out.println(Pattern.compile("[abc]").matcher("ab").matches());   //false
        System.out.println(Pattern.compile("[abc]").matcher("d").matches());    //false
        System.out.println(Pattern.compile("[aA]").matcher("a").matches());     //true
        System.out.println(Pattern.compile("[aA]").matcher("A").matches());     //true
        System.out.println(Pattern.compile("[aA]").matcher("b").matches());     //false
    }
}

程序输出。

true
false
false
true
true
false

3.使用正则表达式匹配字符范围

如果我们想在任何地方匹配一个范围的字符,我们需要使用在范围之间有一个连字符的字符类。例如,'[af]' 将匹配单个字符,它可以是 'a'、'b'、'c'、'd'、'e' 或 'f'。

图案 描述
“[af]” 仅匹配从 'a' 到 'f' 范围内的单个字符。
“[az]” 仅匹配从 'a' 到 'z' 范围内的单个小写字符。
“[AZ]” 仅匹配从 'A' 到 'Z' 范围内的单个大写字符。
“[a-zA-Z]” 仅匹配从 'a' 到 'z' 范围内的单个字符,不区分大小写
“[0-9]” 仅匹配范围从“0”到“9”的单个数字。
import java.util.regex.Pattern; 
public class Main 
{
    public static void main(String[] args) 
    {
        System.out.println(Pattern.compile("[a-f]").matcher("b").matches());    //true
        System.out.println(Pattern.compile("[a-f]").matcher("g").matches());    //false         
        System.out.println(Pattern.compile("[a-zA-Z]").matcher("a").matches()); //true
        System.out.println(Pattern.compile("[a-zA-Z]").matcher("B").matches()); //true
        System.out.println(Pattern.compile("[a-zA-Z]").matcher("4").matches()); //false         
        System.out.println(Pattern.compile("[0-9]").matcher("9").matches());    //true
        System.out.println(Pattern.compile("[0-9]").matcher("91").matches());   //false
    }
}

程序输出。

true
false
true
true
false
true
true

以上就是动力节点小编介绍的"正则表达式任意字符的匹配",希望对大家有帮助,想了解更多可查看Java在线学习。动力节点在线学习教程,针对没有任何Java基础的读者学习,让你从入门到精通,主要介绍了一些Java基础的核心知识,让同学们更好更方便的学习和了解Java编程,感兴趣的同学可以关注一下。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>