Programming Tips - Java: use the Pattern and Matcher classes to extract text?

Date: 2011jan25 Language: Java Q. Java: use the Pattern and Matcher classes to extract text? A. Here's an example:
// Need to escape backslashes from string static final Pattern pattern = Pattern.compile("(\\d+)\\s+(\\w+)\\s+(\\d+)"); // YYYY MMM DD void doMatch(String date_str) { Matcher matcher = pattern.matcher(date_str); if (matcher.matches()) { // Do match and fill group()'s // group(0) is the entire match // groupCount() is the number of groups in the pattern // (even if the match fails) String year = matcher.group(1); // 1 is the first String mon = matcher.group(2); String mday = matcher.group(3); Log.i(Main.LOG_TAG, "year=" + year + " month=" + mon + " mday=" + mday); } }