正则表达式匹配提取字符串 ?! ?=

A:<img src=\"http://localhost:8443/image/20210903/f7dd0a30a2d01bf0a186120f96bf9af6/image7.png\"  />  b.<img src=\"http://localhost:8443/image/20210903/f7dd0a30a2d01bf0a186120f96bf9af6/image7.png\"  />  Z、


        Pattern r = Pattern.compile("([A-Za-z])([::..、])(.+?)(?=((?!p|t)[A-Z_a-z][::..、]))");
        Matcher m = r.matcher("A:<img src=\"http://localhost:8443/image/20210903/f7dd0a30a2d01bf0a186120f96bf9af6/image7.png\"  />  b.<img src=\"http://localhost:8443/image/20210903/f7dd0a30a2d01bf0a186120f96bf9af6/image7.png\"  />  Z、");
//        Pattern r = Pattern.compile("Window(?=95|98|NT|2000|3.1)");
//        Matcher m = r.matcher("Windows95 Window95");
        while (m.find()) {
            System.out.print(m.group(1));
            System.out.print("-");
            System.out.print(m.group(3));
            System.out.println("");
        }

输出

A-<img src="http://localhost:8443/image/20210903/f7dd0a30a2d01bf0a186120f96bf9af6/image7.png" />
b-<img src="http://localhost:8443/image/20210903/f7dd0a30a2d01bf0a186120f96bf9af6/image7.png" />

((?!p|t)[A-Z_a-z][::..、])

这串的意思是匹配不包含pt开头的英文字符且第二个字符为::..、

?=为预查不消耗匹配到的字符,也就是说匹配到了以b.做结尾的字符但是不消耗b.

posted @ 2021-09-08 17:14  曾将  阅读(627)  评论(0编辑  收藏  举报
//目录 欢迎评论,一起学习,对大家有用请点个赞。