正则表达式 元字符 限定符

来自姬鸿昌的知识库
跳到导航 跳到搜索

https://www.bilibili.com/video/BV1Eq4y1E79W?p=11

限定符用于指定其前面的字符和组合项连续出现多少次

符号 含义 示例 说明 匹配输入
* 指定字符重复0次或n次(无要求) (abc)* 仅包含任意个abc的字符串,等效于 \w* abc、abcabcabc
+ 指定字符重复1次或n次(至少1次) m+(abc)* 以至少1个m开头,后接任意个abc的字符串 m、mabc、mabcabc
? 指定字符重复0次或1次(最多1次) m+abc?

注意:abc没有用括号括起来,?就只会作用在离它最近的字符 c

以至少1个m开头,后接ab或abc的字符串 mab、mabc、mmmab、mmabc
{n} 只能输入n个字符 [abcd]{3} 由abcd中字母组成的任意长度为3的字符串 abc、dbc、adc
{n,} 指定至少 n 个匹配 [abcd]{3,} 由abcd中字母组成的任意长度不小于3的字符串 aab、dbc、aaadbc
{n,m} 指定至少n个但不多于m个匹配 [abcd]{3,5} 由abcd中字母组成的任意长度不小于3,不大于5的字符串 abc、abcd、aaaaa、bcdab

注意:贪婪匹配的原则,表达式会尽可能匹配更多的字符

关于限定符 "+" 加号的测试示例

public class PlusSymbioTest {

    public static void main(String[] args) {

        PlusSymbioTest test = new PlusSymbioTest();

        test.test1();

        test.test2();

        test.test3();

    }

    public void test1() {

        String content = "abab";

        String regex = "ab+";

        System.out.println(content.matches(regex));

    }

    public void test2() {

        String content = "abb";

        String regex = "ab+";

        System.out.println(content.matches(regex));

    }

    public void test3() {

        String content = "abab";

        String regex = "(ab)+";

        System.out.println(content.matches(regex));

    }

}
false
true
true

结论:所以默认没有圆括号的情况下限定符应该是只和前面的一个字结合,加括号才代表多个字符连接在一起的子式作为限定符生效的对象。


示例1:在 "1111111" 中找 a{3}

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

public class RegExp05 {

    public static void main(String[] args) {

        String content = "1111111";

        String regStr = "a{3}"; //表示匹配 3 个连续的字符a,等价与 aaa

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {
            System.out.println("找到:"+ matcher.group(0));
        }

    }

}

因为没有对应的内容,所以控制台什么都不输出



示例2:在 "1111111aaahello" 中找 a{3}

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

public class RegExp05 {

    public static void main(String[] args) {

        String content = "1111111aaahello";

        String regStr = "a{3}"; //表示匹配 3 个连续的字符a,等价与 aaa

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {
            System.out.println("找到:"+ matcher.group(0));
        }

    }

}
找到:aaa


示例3:在 "1111111aaahello" 中找 1{4}

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

public class RegExp05 {

    public static void main(String[] args) {

        String content = "1111111aaahello";

        String regStr = "1{4}"; //表示匹配 4 个连续的字符1,等价与 1111

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {
            System.out.println("找到:"+ matcher.group(0));
        }

    }

}
找到:1111



示例4:在 "1111111aaahello" 中找 \\d{2}

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

public class RegExp05 {

    public static void main(String[] args) {

        String content = "1111111aaahello";

        String regStr = "\\d{2}"; //表示匹配 2 个连续的数字字符,等价于 [0-9]{2}

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {
            System.out.println("找到:"+ matcher.group(0));
        }

    }

}
找到:11
找到:11
找到:11



示例5:在 "1111111aaaahello" 中找 a{3,4}

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

public class RegExp05 {

    public static void main(String[] args) {

        String content = "1111111aaaahello";

        //默认是贪婪匹配,尽可能匹配多的
        String regStr = "a{3,4}"; //表示匹配 3-4 个连续的字符 a, aaa 或 aaaa,优先匹配 4 个连续的字符 a

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {
            System.out.println("找到:"+ matcher.group(0));
        }

    }

}
找到:aaaa

注意:默认是贪婪匹配,尽可能匹配更多的字符,匹配出更长的字符串



示例6:在 "1111111aaaahello" 中找 1{4,5}

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

public class RegExp05 {

    public static void main(String[] args) {

        String content = "1111111aaaahello";

        //默认是贪婪匹配,尽可能匹配多的
        String regStr = "1{4,5}"; //表示匹配 4-5 个连续的字符 1, 1111 或 11111,优先匹配 5 个连续的字符 1

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {
            System.out.println("找到:"+ matcher.group(0));
        }

    }

}
找到:11111



示例7:在 "1111111aaaahello" 中找 \\d{2,5}

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

public class RegExp05 {

    public static void main(String[] args) {

        String content = "1111111aaaahello";

        //默认是贪婪匹配,尽可能匹配多的
        String regStr = "\\d{2,5}"; //表示匹配 2-5 个连续的数字字符,优先匹配 5 个连续的数字字符

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {
            System.out.println("找到:"+ matcher.group(0));
        }

    }

}
找到:11111
找到:11

注意:贪婪匹配




示例8:在 "111111aaaahello" 中找 \\d{2,5}

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

public class RegExp05 {

    public static void main(String[] args) {

        String content = "111111aaaahello";

        //默认是贪婪匹配,尽可能匹配多的
        String regStr = "\\d{2,5}"; //表示匹配 2-5 个连续的数字字符,优先匹配 5 个连续的数字字符

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {
            System.out.println("找到:"+ matcher.group(0));
        }

    }

}
找到:11111



示例9:在 "111111aaaahello" 中找 1+

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

public class RegExp05 {

    public static void main(String[] args) {

        String content = "111111aaaahello";

        //默认是贪婪匹配,尽可能匹配多的
        String regStr = "1+"; //表示匹配1个或连续的多个数字字符 1

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {
            System.out.println("找到:"+ matcher.group(0));
        }

    }

}
找到:111111



示例10:在 "111111aaaahello" 中找 \\d+

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

public class RegExp05 {

    public static void main(String[] args) {

        String content = "111111aaaahello";

        //默认是贪婪匹配,尽可能匹配多的
        String regStr = "\\d+"; //表示匹配1个或连续的多个数字字符 相当于[0-9]{1,}、[0-9]+

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {
            System.out.println("找到:"+ matcher.group(0));
        }

    }

}
找到:111111



示例11:在 "111111aaaahello" 中找 1*

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

public class RegExp05 {

    public static void main(String[] args) {

        String content = "111111aaaahello";

        //默认是贪婪匹配,尽可能匹配多的
        String regStr = "1*"; //表示匹配0个或连续的多个数字字符 1

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {
            System.out.println("找到:"+ matcher.group(0));
        }

    }

}
找到:111111
找到:
找到:
找到:
找到:
找到:
找到:
找到:
找到:
找到:
找到:



示例12:在 "a111111aaaahello" 中找 a1?

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

public class RegExp05 {

    public static void main(String[] args) {

        String content = "a111111aaaahello";

        //默认是贪婪匹配,尽可能匹配多的
        String regStr = "a1?"; //表示匹配 单个字符 a 或 a1, ?表示出现0次或1次,最多1次

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {
            System.out.println("找到:"+ matcher.group(0));
        }

    }

}
找到:a1
找到:a
找到:a
找到:a
找到:a


示例13:在 "a211111aaaahello" 中找 a1?

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

public class RegExp05 {

    public static void main(String[] args) {

        String content = "a211111aaaahello";

        //默认是贪婪匹配,尽可能匹配多的
        String regStr = "a1?"; //表示匹配 单个字符 a 或 a1, ?表示出现0次或1次,最多1次

        Pattern pattern = Pattern.compile(regStr);

        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {
            System.out.println("找到:"+ matcher.group(0));
        }

    }

}
找到:a
找到:a
找到:a
找到:a
找到:a