![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e433. Setting Case Sensitivity in a Regular ExpressionBy default, a pattern is case-sensitive. By adding a flag, a pattern can be made case-insensitive. It is also possible to control case sensitivity within a
pattern using the inline modifier The inline modifier can also contain pattern characters using
the form 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 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 // Use a character set matchFound = pattern.matches("[a-c]+", "aBc"); // false matchFound = pattern.matches("(?i)[a-c]+", "aBc"); // true
e435. Compiling a Pattern with Multiple Flags
© 2002 Addison-Wesley. |