public class TestDemo { public static void main(String[] args) { //匹配以∈{開頭,中間7個字符 ,以}∈結尾的字符串 boolean isMatch1 = Pattern.matches("^∈\\{+.{7}\\}∈$", "∈{ddddddd}∈"); System.out.println(isMatch1); boolean isMatch = Pattern.matches("^∞+.{7}∞$", "∞ddddddd∞"); // .為任意字符串, "^∞+\\d{7}∞$" 表示中間7位字符必須為數字 Pattern p = Pattern.compile("^∞+.{7}∞$"); Matcher matcher = p.matcher("∞1212a21∞"); System.out.println(matcher.find()); } }