non-capturing groups
non-capturing groups 知识点铺垫1
public class Test10_1 {
public static void main(String[] args) {
Pattern p = Pattern.compile(".{3}");
String s = "44a66b";
Matcher m = p.matcher(s);
while (m.find()) {
p(m.group());
}
}
public static void p(Object o) {
System.out.println(o);
}
}
non-capturing groups 知识点铺垫2
public class Test10_2 {
public static void main(String[] args) {
Pattern p = Pattern.compile(".{2}a");
String s = "44a66b";
Matcher m = p.matcher(s);
while (m.find()) {
p(m.group());
}
}
public static void p(Object o) {
System.out.println(o);
}
}
non-capturing groups 知识点铺垫3
public class Test10_3 {
public static void main(String[] args) {
Pattern p = Pattern.compile(".{3}(?=a)");
String s = "444a66b";
Matcher m = p.matcher(s);
while (m.find()) {
p(m.group());
}
System.out.println();
Pattern pp = Pattern.compile(".{3}a");
String ss = "444a66b";
Matcher mm = pp.matcher(ss);
while (mm.find()) {
p(mm.group());
}
System.out.println();
Pattern ppp = Pattern.compile("(?=a).{3}");
String sss = "444a66b";
Matcher mmm = ppp.matcher(sss);
while (mmm.find()) {
p(mmm.group());
}
}
public static void p(Object o) {
System.out.println(o);
}
}
non-capturing groups 知识点铺垫4
public class Test10_4 {
public static void main(String[] args) {
Pattern p = Pattern.compile("(?=a).{3}");
String s = "444a66b";
Matcher m = p.matcher(s);
while (m.find()) {
p(m.group());
}
}
public static void p(Object o) {
System.out.println(o);
}
}
non-capturing groups 知识点铺垫5
public class Test10_5 {
public static void main(String[] args) {
Pattern p = Pattern.compile("(?!a).{3}");
String s = "444a66b";
Matcher m = p.matcher(s);
while (m.find()) {
p(m.group());
}
System.out.println();
Pattern pp = Pattern.compile(".{3}(?!a)");
String ss = "444a66b";
Matcher mm = pp.matcher(ss);
while (mm.find()) {
p(mm.group());
}
}
public static void p(Object o) {
System.out.println(o);
}
}
non-capturing groups 知识点铺垫6
public class Test10_6 {
public static void main(String[] args) {
Pattern p = Pattern.compile(".{3}(?<!a)");
String s = "444a66b";
Matcher m = p.matcher(s);
while (m.find()) {
p(m.group());
}
System.out.println();
Pattern pp = Pattern.compile(".{3}(?<=a)");
String ss = "444a66b";
Matcher mm = pp.matcher(ss);
while (mm.find()) {
p(mm.group());
}
}
public static void p(Object o) {
System.out.println(o);
}
}
non-capturing groups 完整版
public class Test10 {
public static void main(String[] args) {
Pattern p = Pattern.compile(".{3}");
String s = "44a66b";
Matcher m = p.matcher(s);
while (m.find()) {
p(m.group());
}
System.out.println();
Pattern pp = Pattern.compile(".{2}a");
String ss = "44a66b";
Matcher mm = pp.matcher(ss);
while (mm.find()) {
p(mm.group());
}
System.out.println();
Pattern ppp = Pattern.compile(".{3}(?=a)");
String sss = "444a66b";
Matcher mmm = ppp.matcher(sss);
while (mmm.find()) {
p(mmm.group());
}
System.out.println();
Pattern pppp = Pattern.compile(".{3}a");
String ssss = "444a66b";
Matcher mmmm = pppp.matcher(ssss);
while (mmmm.find()) {
p(mmmm.group());
}
System.out.println();
Pattern ppppp = Pattern.compile("(?=a).{3}");
String sssss = "444a66b";
Matcher mmmmm = ppppp.matcher(sssss);
while (mmmmm.find()) {
p(mmmmm.group());
}
System.out.println();
Pattern pppppp = Pattern.compile("(?!a).{3}");
String ssssss = "444a66b";
Matcher mmmmmm = pppppp.matcher(ssssss);
while (mmmmmm.find()) {
p(mmmmmm.group());
}
System.out.println();
Pattern ppppppp = Pattern.compile(".{3}(?!a)");
String sssssss = "444a66b";
Matcher mmmmmmm = ppppppp.matcher(ssssss);
while (mmmmmmm.find()) {
p(mmmmmmm.group());
}
System.out.println();
Pattern pppppppp = Pattern.compile(".{3}(?<!a)");
String ssssssss = "444a66b";
Matcher mmmmmmmm = pppppppp.matcher(ssssssss);
while (mmmmmmmm.find()) {
p(mmmmmmmm.group());
}
System.out.println();
Pattern ppppppppp = Pattern.compile(".{3}(?<=a)");
String sssssssss = "444a66b";
Matcher mmmmmmmmm = ppppppppp.matcher(sssssssss);
while (mmmmmmmmm.find()) {
p(mmmmmmmmm.group());
}
}
public static void p(Object o) {
System.out.println(o);
}
}
back refenrences(向前引用)
public class Test11 {
public static void main(String[] args) {
Pattern p = Pattern.compile("(\\d\\d)\\1");
String s = "1212";
Matcher m = p.matcher(s);
p(m.matches());
System.out.println();
Pattern pp = Pattern.compile("(\\d(\\d))\\2");
String ss = "1212";
Matcher mm = pp.matcher(ss);
p(mm.matches());
System.out.println();
Pattern ppp = Pattern.compile("(\\d(\\d))\\2");
String sss = "122";
Matcher mmm = ppp.matcher(sss);
p(mmm.matches());
}
public static void p(Object o) {
System.out.println(o);
}
}
flags的简写
public class Test12 {
public static void main(String[] args) {
p("Java".matches("(?i)(java)"));
}
public static void p(Object o) {
System.out.println(o);
}
}