在正则表达式中设置区分大小写
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { /* 时代Java 提 供 */ public void m() { CharSequence inputStr = "Abc"; String patternStr = "abc"; // Compile with case-insensitivity Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(inputStr); boolean matchFound = matcher.matches(); // true // Use an inline modifier matchFound = pattern.matches("abc", "aBc"); // false matchFound = pattern.matches("(?i)abc", "aBc"); // true// from n o w j a v a . c o m - 时 代 Java matchFound = pattern.matches("a(?i)bc", "aBc"); // true // Use enclosing form matchFound = pattern.matches("((?i)a)bc", "aBc"); // false matchFound = pattern.matches("(?i:a)bc", "aBc"); // false matchFound = pattern.matches("a((?i)b)c", "aBc"); // true matchFound = pattern.matches("a(?i:b)c", "aBc"); // true