Controlling Case in match( ) and subst( )

Problem

You want to find text regardless of case.

Solution

Use the flags static int variable RE.MATCH_CASEINDEPENDENT to indicate that matching should be case-independent (“fold” or ignore differences in case) or RE_MATCH_NORMAL to request normal, case-sensitive matching behavior. These flags can either be passed to the RE constructor method, as in:

// CaseMatch.java
RE r = new RE(pattern, RE.MATCH_CASEINDEPENDENT);
r.match(input);        // will match case-insensitively

or passed to the RE’s setMatchFlags( ) method before calling match( ), as in:

r.setMatchFlags(RE.MATCH_NORMAL);
r.match(input);        // will match case-sensitively
If we print the results of both match operations
+ jikes +E -d . CaseMatch.java
+ java CaseMatch
MATCH_CASEINDEPENDENT match true
MATCH_NORMAL match was false

The full source for this example is online as CaseMatch.java.

Get Java Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.