非常有用的一个正则(?=pattern)

以前在java中的在正则匹配的时候,总会多一个正则匹配的字符,但是用了(?=pattern)这个就可以方便很多

/**
 * @author WGR
 * @create 2020/5/6 -- 18:08
 */
public class Test {

    public static void main(String[] args) {
        String str = "我是谁123得到214号";
        String pattern = "\d+(?=号)";

        // 创建 Pattern 对象
        Pattern r = Pattern.compile(pattern);

        // 现在创建 matcher 对象
        Matcher m = r.matcher(str);
        if (m.find( )) {
            System.out.println("Found value: " + m.group() );
        } else {
            System.out.println("NO MATCH");
        }
    }
}

原文地址:https://www.cnblogs.com/dalianpai/p/12838145.html