正則表示式匹配提取字串 ?! ?=
阿新 • • 發佈:2021-09-08
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.
歡迎評論,一起學習,對大家有用請點個贊。