The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: Bonus
Lesson: Regular Expressions

## Methods of the Pattern Class

> Until now, we've only used the test harness to create `Pattern` objects in their most basic form. This section explores advanced techniques such as creating patterns with flags and using embedded flag expressions. It also explores some remaining methods we haven't discussed yet.

> ### Creating a Pattern with Flags

> The `Pattern` class defines an alternate `compile` method that accepts a set of flags affecting the way the pattern is matched. The flags parameter is a bit mask that may include any of the following public static fields:
> - `Pattern.CANON_EQ`
> - `Pattern.CASE_INSENSITIVE`
> - `Pattern.COMMENTS`
> - `Pattern.DOTALL`
> - `Pattern.MULTILINE`
> - `Pattern.UNICODE_CASE`
> - `Pattern.UNIX_LINES`
> In the following steps we will modify the test harness, `RegexTestHarness` to create a pattern with case-insensitive matching.

> First, modify the code to call the alternate version of `compile`:

> >     pattern = Pattern.compile(REGEX,Pattern.CASE_INSENSITIVE);

> Then edit your input file, `regex.txt`, to contain the following:

> >     dog
> >     DoGDOg

> Finally, compile and run the test harness to get the following results:

> >     Current REGEX is: dog
> >     Current INPUT is: DoGDOg
> >     I found the text "DoG" starting at index 0 and ending at index 3.
> >     I found the text "DOg" starting at index 3 and ending at index 6.

> As you can see, the string literal "dog" matches both occurances, regardless of case. To compile a pattern with multiple flags, separate the flags to be included using the bitwise OR operator (`|`):

> >     pattern = Pattern.compile("[az]$", Pattern.MULTILINE | Pattern.UNIX_LINES);

> Note that for clarity you could also specify an `int` variable:

> >     final int flags = Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE;
> >     Pattern pattern = Pattern.compile("aa", flags);

> ### Embedded Flag Expressions

> It's also possible to enable various flags using embedded flag expressions. Embedded flag expressions are an alternative to the two-argument version of `compile`, and are specified in the regular expression itself. The following example uses the original test harness, `RegexTestHarness.java` with the embedded flag expression `(?i)` to enable case-insensitive matching.

> >     Current REGEX is: (?i)foo
> >     Current INPUT is: FOOfooFoOfoO
> >     I found the text "FOO" starting at index 0 and ending at index 3.
> >     I found the text "foo" starting at index 3 and ending at index 6.
> >     I found the text "FoO" starting at index 6 and ending at index 9.
> >     I found the text "foO" starting at index 9 and ending at index 12.

> Once again, all matches succeed regardless of case.

> The embedded flag expressions that correspond to `Pattern`'s publicly-accessible fields are presented in the following table:

> > Constant | Equivalent Embedded Flag Expression
> > `Pattern.CANON_EQ` | None
> > `Pattern.CASE_INSENSITIVE` | `(?i)`
> > `Pattern.COMMENTS` | `(?x)`
> > `Pattern.MULTILINE` | `(?m)`
> > `Pattern.DOATALL` | `(?s)`
> > `Pattern.UNICODE_CASE` | `(?u)`
> > `Pattern.UNIX_LINES` | `(?d)`

> ### Using the `matches(String,CharSequence)` Method

> The `Pattern` class defines a convenient `matches` method that allows you to quickly check if a pattern is present in a given input string. As with all public static methods, you should call `matches` with its class name, such as `Pattern.matches("\\d","1");` In this example, the method returns true, because the digit "1" matches the regular expression `\d`.

> ### Using the `split(String)` method

> The `split` method is a great tool for gathering the text that lies on either side of the pattern that's been matched. As shown below in the `SplitTest` code, the `split` method could extract the words "`one two three four five`" from the string "`one:two:three:four:five`":

> >     import java.util.regex.*;

> >     public final class SplitTest {

> >         private static String REGEX = ":";
> >         private static String INPUT = "one:two:three:four:five";

> >         public static void main(String[] argv) {
> >             Pattern p = Pattern.compile(REGEX);
> >             String[] items = p.split(INPUT);
> >             for(int i=0;i<items.length;i++) {
> >                 System.out.println(items[i]);
> >             }
> >         }
> >     }

> >     OUTPUT:

> >     one
> >     two
> >     three
> >     four
> >     five

> For simplicity, we've matched a string literal, the colon (`:`) instead of a complex regular expression. Since we're still using `Pattern` and `Matcher` objects, you can use split to get the text that falls on either side of any regular expression. Here's the same example, `SplitTest2`, modified to split on digits instead:

> >     import java.util.regex.*;

> >     public final class SplitTest2 {

> >         private static String REGEX = "\\d";
> >         private static String INPUT = "one9two4three7four1five";

> >         public static void main(String[] argv) {
> >             Pattern p = Pattern.compile(REGEX);
> >             String[] items = p.split(INPUT);
> >             for(int i=0;i<items.length;i++) {
> >                 System.out.println(items[i]);
> >             }
> >         }
> >     }

> >     OUTPUT:

> >     one
> >     two
> >     three
> >     four
> >     five

> ## Pattern Method Equivalents in `java.lang.String`

> Regular expression support has also been introduced to `java.lang.String` through several methods that mimic the behavior of `java.util.regex.Pattern`. For convenience, key excerpts from their API are presented below.
> - `public boolean matches(String regex)`: Tells whether or not this string matches the given regular expression. An invocation of this method of the form `str.matches(regex)` yields exactly the same result as the expression `Pattern.matches(regex, str)`.

> - `public String[] split(String regex, int limit)`: Splits this string around matches of the given regular expression. An invocation of this method of the form `str.split(regex, n)` yields the same result as the expression `Pattern.compile(regex).split(str, n)`

> - `public String[] split(String regex)`: Splits this string around matches of the given regular expression. This method works the same as if you invoked the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are not included in the resulting array.

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.