一、Pattern与Matcher
String regex = "\\(";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
-
Pattern.compile(regex):返回一个正则表达式
-
pattern.matcher(str):返回一个matcher,pattern为“regex”
-
Pattern.matches(regex, str):字符串与正则完全匹配则返回true
-
matcher.matches():同上
-
matcher.lookingAt():字符串匹配正则,若在最前返回true
-
matcher.find():字符串匹配到正则,返回true
System.out.println("pattern:" + pattern); System.out.println("matcher:" + matcher); System.out.println("Pattern.matches:" + Pattern.matches(regex, str)); System.out.println("matcher.matches:" + matcher.matches()); System.out.println("matcher.lookingAt:" + matcher.lookingAt()); System.out.println("matcher.find:" + matcher.find()); pattern:\( matcher:java.util.regex.Matcher[pattern=\( region=0,49 lastmatch=] Pattern.matches:fal