如您所链接的文章所述,它取决于您调用的函数。如果要默认添加^和$,请使用
String#matches或
Matcher#matches。如果您不想这样做,请改用
Matcher#find方法。
import java.util.regex.*;public class Example{ public static void main(String[] args) { System.out.println("Matches: " + "abc".matches("a+")); Matcher matcher = Pattern.compile("a+").matcher("abc"); System.out.println("Find: " + matcher.find()); }}输出:
Matches: falseFind: true



