从零开始学习正则表达式 基础篇

正则表达式(Regular Expression,简称 regex 或 regexp)是一种用来匹配字符串中字符组合的模式。它常用于文本搜索和文本替换操作。

一. 匹配模式

  • 全部匹配:需要整个字符串完全符合正则表达式模式。在Java中使用 String.matches() 实现。
  • 部分匹配:只需要字符串中的某个子串符合正则表达式模式。在Java中使用 Pattern 和 Matcher 的 find() 方法实现。

一.如何进行基础字符匹配

完全匹配模式
//普通字符匹配:字母、数字等普通字符在正则表达式中代表它们本身。
// 例如,正则表达式 abc 可以匹配字符串 "abc"。

    public static void main(String[] args) {
        // 定义正则表达式
        String pattern = "abc";
        // 要匹配的字符串
        String str = "abc";
        // 输出结果
        System.out.println(str.matches(pattern));
        String str1 = "abcd";
        System.out.println(str1.matches(pattern));
}
---------------------------------------------------------------------
true
false
---------------------------------------------------------------------
部分匹配模式
    public static void main(String[] args) {
        String pattern = "abc";
        String str1 = "abc"; // 部分匹配
        String str2 = "abcd";  // 部分匹配
        Pattern compiledPattern = Pattern.compile(pattern);
        System.out.println(matchesPattern(compiledPattern, str1)); // 输出:true
        System.out.println(matchesPattern(compiledPattern, str2)); // 输出:true
}

    private static boolean matchesPattern(Pattern pattern, String input) {
        Matcher matcher = pattern.matcher(input);
        return matcher.find(); // 使用 find() 方法检查部分匹配
    }
---------------------------------------------------------------------
true
true
---------------------------------------------------------------------

二.认识元字符

常见的元字符有
. 匹配除换行符以外的任意单个字符。
^ 匹配字符串开始的位置。
$ 匹配字符串结束的位置。
* 匹配前面的元素零次或多次。
+ 匹配前面的元素一次或多次。
? 匹配前面的元素零次或一次。
{n,m} 匹配前面的元素至少 n 次,但不超过 m 次。
[] 定义字符类,匹配方括号内的任意一个字符。
| 逻辑或,匹配左边或右边的表达式。
() 分组,捕获匹配的子表达式。

元字符在正则表达式中有特殊的含义。

第一个元字符 
完全匹配模式
//  . 匹配除换行符以外的任意单个字符。

    public static void main(String[] args) {
        // 定义正则表达式
        String pattern = ".";
        // 要匹配的字符串
        String str = "a";
        String str1 = "#";

        // 输出结果
        System.out.println(str.matches(pattern));
        System.out.println(str1.matches(pattern));
    
}
---------------------------------------------------------------------
true
true
---------------------------------------------------------------------

部分匹配模式
    public static void main(String[] args) {
        String pattern = ".";
        String str1 = "abc"; // 部分匹配
        String str2 = "abcd";  // 部分匹配
        Pattern compiledPattern = Pattern.compile(pattern);
        System.out.println(matchesPattern(compiledPattern, str1)); // 输出:true
        System.out.println(matchesPattern(compiledPattern, str2)); // 输出:true
    }

    private static boolean matchesPattern(Pattern pattern, String input) {
        Matcher matcher = pattern.matcher(input);
        return matcher.find(); // 使用 find() 方法检查部分匹配
    }
第二个元字符 
完全匹配  部分匹配也要以a开头

// ^ 匹配字符串开始的位置。
    public static void main(String[] args) {
        // 定义正则表达式   解释 ^匹配字符串开头第一个位置 是否是a  并且以a开头的后面追加任意数量任意字符除换
        //行外 
        String pattern = "^a.*";
        // 要匹配的字符串
        String str = "a111";
        String str1 = "baaa";

        // 输出结果
        System.out.println(str.matches(pattern));
        System.out.println(str1.matches(pattern));

}
---------------------------------------------------------------------
true
false
---------------------------------------------------------------------
第三个元字符

// $ 匹配字符串结束的位置。 
完全匹配 部分匹配也要以b结尾
    public static void main(String[] args) {
        // 解释  任意数量任意字符除换行符外 以b结尾的字符串
        String  pattern = ".*b$";

        // 要匹配的字符串
        String str = "a111b";
        String str1 = "baaac";

        // 输出结果
        System.out.println(str.matches(pattern)); //ture
        System.out.println(str1.matches(pattern)); //false
}
第四个元字符

// * 匹配前面的元素零次或多次。
完全匹配
    public static void main(String[] args) {
        String pattern = "a*";
        
        String str1 = "aavv"; // 不会完全匹配,因为字符串中有非 "a" 的字符 
        String str2 = "aaa";  // 会匹配,因为只有 "a"
        String str3 = "bca";  // 不会完全匹配,因为字符串以 "b" 开头
        String str4 = "";     // 会匹配,空字符串符合零次 "a"
        String str5 = "aaab"; // 不会完全匹配,因为字符串末尾有 "b"

        System.out.println(str1.matches(pattern)); // 输出:false
        System.out.println(str2.matches(pattern)); // 输出:true
        System.out.println(str3.matches(pattern)); // 输出:false
        System.out.println(str4.matches(pattern)); // 输出:true
        System.out.println(str5.matches(pattern)); // 输出:false
}

部分匹配
        String pattern = "a*";
        String str1 = "aavv"; // 部分匹配 子串包含 0次或多次 a
        String str2 = "aaa";  // 部分匹配 子串包含 0次或多次 a
        String str3 = "bca";  // 部分匹配 子串包含 0次或多次 a
        String str4 = "";     // 部分匹配 子串包含 0次或多次 a
        String str5 = "aaab"; // 部分匹配 子串包含 0次或多次 a
        Pattern compiledPattern = Pattern.compile(pattern);

        System.out.println(matchesPattern(compiledPattern, str1));  // true
        System.out.println(matchesPattern(compiledPattern, str2));  // true
        System.out.println(matchesPattern(compiledPattern, str3));  // true
        System.out.println(matchesPattern(compiledPattern, str4));  // true
        System.out.println(matchesPattern(compiledPattern, str5));  // true
第五个元字符
// + 匹配前面的元素一次或多次。
完全匹配
    public static void main(String[] args) {
        System.out.println("--------------------------------------------");
        String pattern = "a+";
        String str1 = "aavv"; // 除了a包含其他字符 不匹配
        String str2 = "aaa";  // 除了a不包含其他字符 并且a出现了  一次或多次 匹配
        String str3 = "bca";  // 除了a包含其他字符 不匹配
        String str4 = "";     // 没有一个a 不匹配
        String str5 = "aaab"; // 除了a包含其他字符 不匹配


        System.out.println(str1.matches(pattern));  // false
        System.out.println(str2.matches(pattern));  // true
        System.out.println(str3.matches(pattern));  // false
        System.out.println(str4.matches(pattern));  // false
        System.out.println(str5.matches(pattern));  // false
    }



部分匹配
    public static void main(String[] args) {
        String pattern = "a+";
        String str1 = "aavv"; // 出现一次 或多次 a  部分匹配
        String str2 = "aaa";  // 出现一次 或多次 a  部分匹配
        String str3 = "bca";  // 出现一次 或多次 a  部分匹配
        String str4 = "";     // 没有一个a 不匹配
        String str5 = "aaab"; // 出现一次 或多次 a  部分匹配
        Pattern compiledPattern = Pattern.compile(pattern);

        System.out.println(matchesPattern(compiledPattern, str1));  // true
        System.out.println(matchesPattern(compiledPattern, str2));  // true
        System.out.println(matchesPattern(compiledPattern, str3));  // true
        System.out.println(matchesPattern(compiledPattern, str4));  // false
        System.out.println(matchesPattern(compiledPattern, str5));  // true
    }
// ? 匹配前面的元素零次或一次。
完全匹配
    public static void main(String[] args) {
        System.out.println("--------------------------------------------");
        String pattern = "a?";
        String str1 = "aavv"; // a出现了2次 不匹配 并且除了a还有其他字符
        String str2 = "aaa";  // // a出现了多次 不匹配
        String str3 = "bca";  // 除了a包含其他字符 不匹配
        String str4 = "";     // 没有一个a 匹配
        String str5 = "aaab"; // 出现了多次 除了a包含其他字符 不匹配


        System.out.println(str1.matches(pattern));  // false
        System.out.println(str2.matches(pattern));  // false
        System.out.println(str3.matches(pattern));  // false
        System.out.println(str4.matches(pattern));  // true
        System.out.println(str5.matches(pattern));  // false
    }
部分匹配
    public static void main(String[] args) {
        String pattern = "a?";
        String str1 = "aavv"; // 部分匹配 子串满足 a出现 0 或 1 次
        String str2 = "aaa";  // 部分匹配 子串满足 a出现 0 或 1 次
        String str3 = "bca";  // 部分匹配 子串满足 a出现 0 或 1 次
        String str4 = "";     // 部分匹配 子串满足 a出现 0 或 1 次
        String str5 = "aaab"; // 部分匹配 子串满足 a出现 0 或 1 次
        Pattern compiledPattern = Pattern.compile(pattern);

        System.out.println(matchesPattern(compiledPattern, str1));  // true
        System.out.println(matchesPattern(compiledPattern, str2));  // true
        System.out.println(matchesPattern(compiledPattern, str3));  // true
        System.out.println(matchesPattern(compiledPattern, str4));  // true
        System.out.println(matchesPattern(compiledPattern, str5));  // true
    }

未完待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值