O'Reilly logo

Java Cookbook by Ian F. Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required