Finding Matching Text Using a Regular Expression


String pattern = "[TJ]im"; Pattern regPat = Pattern.compile(pattern); String text = "This is jim and Timothy."; Matcher matcher = regPat.matcher(text); if (matcher.find()) {    String matchedText = matcher.group(); }



In this pattern, we make use of the Pattern and the Matcher classes. We use the static compile() method of the Pattern class to compile a pattern string into a Pattern object. Once we have the regPat Pattern object, we use the matcher() method, passing in the text string we want to match against. The matcher() method returns an instance of the Matcher class. Finally, we call the group() method of the Matcher class to obtain the matched text. The matched text in this phrase will be the character string "Tim". Note that the string "jim" will not match because regular expressions are case sensitive by default. To perform a non-case sensitive search, we could slightly modify the code as shown here:

String patt = "[TJ]im"; Pattern regPat =    Pattern.compile(patt, Pattern.CASE_INSENSITIVE); String text = "This is jim and Timothy."; Matcher matcher = regPat.matcher(text); if (matcher.find()) {    String matchedText = matcher.group(); }


This matched text in this code will now be the character string "jim". Because the match is now non-case sensitive, the first match "jim" occurs before the match on "Tim".

Notice that the only difference in this example from our original phrase is that we've added an additional parameter to the compile() method when creating our Pattern object. Here, we pass the CASE_INSENSITIVE flag to denote that we want matching to be performed as non-case sensitive. When we do not include this flag, the default behavior is to perform case sensitive matching.

If your code is required to run in different locales, you would also want to pass the Unicode case flag. So the compile line would look like this in that case:

Pattern regPat =    Pattern.compile(pattern,                    Pattern.CASE_INSENSITIVE |                    Pattern.UNICODE_CASE);


Notice how we pass multiple flags to the compile() method by logically ORing them together. Pattern flags must be passed at the time the Pattern is first created using the compile() method. Once a Pattern object is created, it is immutablemeaning that it cannot be changed in any way.

In our examples so far, we've used the find() method of the Matcher class to find the first match in our input string. The find() method can be called repeatedly to return successive matches in the input string. The find() method will return true as long as a match is found. It will return false when it does not find a match. If you call find() again after having returned false, it will reset and find the first match again. There is an alternative find() method that takes an int parameter specifying an index from which to start a search from. In all other ways, this find() method behaves identically to the find() method without parameters.

There is also an alternative for getting the match result. We've been using the method group() on the Matcher class. There are also useful methods named start() and end(). The start() method will return the index at the beginning of the previous match. Then the end() method will return the index after the last character matched.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net