8.6. Other Pattern MethodsIn addition to the main compile factories, the Pattern class contains some helper methods:
8.6.1. Pattern's split Method, with One Argument
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 matchesIf 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 . |