Section 8.6. Other Pattern Methods


8.6. Other Pattern Methods

In addition to the main compile factories, the Pattern class contains some helper methods:



split

The two forms of this method are covered in detail, starting on the facing page.



String pattern()

This method returns the regular-expression string argument used to create the pattern.



String toString()

This method, added in Java 1.5, is a synonym for the pattern method.



int flags()

This method returns the flags (as an integer) passed to the compile factory when the pattern was created.



static String quote( String text )

This static method, added in Java 1.5, returns a string suitable for use as a regular-expression argument to Pattern.compile that matches the literal text provided as the argument. For example, Pattern.quote("main()") returns the string ' \Qmain()\E ', which, when used as a regular expression, is interpreted as \Qmain()\E , which matches the original argument: ' main() '.



static boolean matches( String regex , CharSequence text )

This static method returns a Boolean indicating whether the regex can exactly match the text (which, as with the argument to the matcher method, can be a String or any object implementing CharSequence ˜ 373). Essentially, this is:

 Pattern.compile(  regex  ).matcher(  text  ).matches(); 

If you need to pass compile options, or gain access to more information about the match than simply whether it was successful, you'll have to use the methods described earlier.

If this method will be called many times (for instance, from within a loop or other code invoked frequently), you'll find it much more efficient to precompile the regex into a Pattern object that you then use each time you actually need to use the regex.

8.6.1. Pattern's split Method, with One Argument



String[] split( CharSequence text )

This pattern method accepts text (a CharSequence ) and returns an array of strings from text that are delimited by matches of the pattern's regex. This functionality is also available via the String class split method.

This trivial example

 String[]  result  = Pattern.compile("\.").split("209.204.146.22"); 

returns an array of the four strings (' 209 ', ' 204 ', ' 146 ', and ' 22 ') that are separated by the three matches of \. in the text. This simple example splits on only a single character, but you can split on any regular expression. For example, you might approximate splitting a string into "words by splitting on non-alphanumerics:

 String[]  result  = Pattern.compile("\W+").split(  Text  ); 

When given a string such as ' ', it returns the four strings (' What ', ' s ', ' up ', and ' Doc ') delimited by the three matches of the regex. (If you had non-ASCII text, you'd probably want to use \P{L}+ , or perhaps [^\p{L}\p{N}_] , as the regex, instead of \W+ ˜ 367.)

8.6.2. Empty elements with adjacent matches

If the regex can match at the beginning of the text , the first string returned by split is an empty string (a valid string, but one that contains no characters ). Similarly, if the regex can match two or more times in a row, empty strings are returned for the zero-length text "separated" by the adjacent matches. For example,

 String[]  result  = Pattern.compile("\s*,\s*").split(", one, two , ,, 3"); 

splits on a comma and any surrounding whitespace, returning an array of five strings: an empty string, ' one ', ' two ', two empty strings, and ' 3 '.

Finally, any empty strings that might appear at the end of the list are suppressed:

 String[]  result  = Pattern.compile(":").split(":xx:"); 

This produces only two strings: an empty string and ' xx '. To keep trailing empty elements, use the two-argument version of split , described next .



Mastering Regular Expressions
Mastering Regular Expressions
ISBN: 0596528124
EAN: 2147483647
Year: 2004
Pages: 113

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