Skip to Main Content
Java Cookbook
book

Java Cookbook

by Ian F. Darwin
June 2001
Intermediate to advanced content levelIntermediate to advanced
888 pages
21h 1m
English
O'Reilly Media, Inc.
Content preview from Java Cookbook

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.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Practical Cloud-Native Java Development with MicroProfile

Practical Cloud-Native Java Development with MicroProfile

Emily Jiang, Andrew McCright, John Alcorn, David Chan, Alasdair Nottingham
Distributed Computing in Java 9

Distributed Computing in Java 9

Raja Malleswara Rao Malleswara Rao Pattamsetti

Publisher Resources

ISBN: 0596001703Supplemental ContentCatalog PageErrata