Case

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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.