Course contentsShow
← Java
Lesson 2312 of 2,87044. Regular ExpressionsFree lesson

CASE_INSENSITIVE Flag: Ignoring Case

Use Pattern.CASE_INSENSITIVE to match letters regardless of case, and understand its Unicode implications.

CASE_INSENSITIVE Flag: Ignoring Case

You'll learn how to make regex patterns match text regardless of uppercase or lowercase letters.

What Is Case-Insensitive Matching?

By default, regex patterns in Java distinguish between uppercase and lowercase letters—"hello" won't match "HELLO". The Pattern.CASE_INSENSITIVE flag tells the regex engine to ignore case differences, treating 'a' and 'A' as equivalent throughout your entire pattern.

Think of it like a search function with a "Match case" checkbox unchecked—it finds results regardless of capitalization.

Using the Flag

Pass Pattern.CASE_INSENSITIVE when compiling your pattern:

Pattern pattern = Pattern.compile("hello", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("HELLO World");
System.out.println(matcher.find()); // true

This matches "hello", "Hello", "HELLO", "HeLLo", and any other case variation.

Unicode Implications

The flag respects Unicode case folding rules, not just ASCII letters. It handles international characters properly:

Pattern pattern = Pattern.compile("café", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("CAFÉ");
System.out.println(matcher.matches()); // true

Characters like 'é' and 'É' are correctly treated as case variants. However, some Unicode characters have complex case-folding behavior—Turkish dotted/dotless 'i' being a famous example—which may require UNICODE_CASE flag in combination for full correctness.

When to Use It

Use CASE_INSENSITIVE when:

  • Validating user input where case shouldn't matter (usernames, commands)
  • Searching text where case is irrelevant (keyword searches)
  • Parsing formats that are case-insensitive by specification

Key Takeaway: The Pattern.CASE_INSENSITIVE flag makes your regex ignore case differences throughout the entire pattern, working correctly with both ASCII and most Unicode characters.