java正则表达式

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


public class Main {
    private static final String REGEX = "[0-9a-z]{2}(?=ab)";
    private static final String INPUT = "23443ab549abe";

    public static void main( String args[] ){
        // s+ 可以匹配多个空格
        // ^ 定义了以什么开始
        // d+ 匹配一个或多个数字
        // ? 设置括号内的选项是可选的
        // . 匹配 "."

        //在 Java 中,\ 表示:我要插入一个正则表达式的反斜线,所以其后的字符具有特殊的意义。
        //pattern 对象是一个正则表达式的编译表示。
        String str = "naabb     sdfdf";
        String[] s = str.split("\s+");
        for (int i = 0; i < s.length; i++) {
            System.out.println(s[i]);
        }
        System.out.println(Pattern.matches(REGEX, INPUT));
        Pattern p = Pattern.compile(REGEX);
        //Matcher 对象是对输入字符串进行解释和匹配操作的引擎。
        Matcher m = p.matcher(INPUT); // 获取 matcher 对象
        int count = 0;
        while(m.find()) {
            count++;
            System.out.println("Match number "+count);
            int i = m.start(), j = m.end();

            System.out.println(INPUT.substring(i,j));
        }
    }


}
原文地址:https://www.cnblogs.com/lixyuan/p/13751768.html