import java.io.FileNotFoundException; import java.util.Arrays; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; public class Exam { public static void main(String[] args) throws FileNotFoundException { new Exam().f(); } void f() throws FileNotFoundException{ String in = "aabcabcabcdef"; // String reg = "(abc)"; // String reg = "(abcabc)"; String reg = "(abc){2,}";//{n,}是种特殊情况,先匹配一个(abc),因此即使只匹配一个(abc),Matcher也不会Reset 》 具体则涉及到组(Group)的概念,没有任何组匹配到输入时,将Reset Pattern p = Pattern.compile(reg); List l = Arrays.asList(p.split(in)).stream().map(x -> f(x)).collect(Collectors.toList()); for (Object object : l) { System.out.println(((Person)object).name); } Matcher m = p.matcher(in); // int i = 0; // while(m.find(i)){ // System.out.print(m.group()+" "); // i++; // } // System.out.println(); System.out.println(m.find(5));//有几个临界点需要注意 5 8 // System.out.println(m.find(0)+" "+m.group()); while(m.find()){ System.out.println(m.group()); //疑问: //这里在Matcher.find(5)==false之后仍然不输出, //但是在Matcher,find(8)==false之后突然开始输出。 } System.out.println(m.find(0)+" "+m.group());//有几个临界点需要注意 boolean b = Pattern.matches(reg, in); System.out.println(b); } Object f(String x){ System.out.println(x+" in List"); return new Person(x); } } class Person{ String name; Person(String name){ this.name = "person:"+name ; } }