/** * Convenience method to take all of the non-null matching groups in a regex Matcher and return * them as a concatenated string. * * @param matcher The Matcher object from which grouped text will be extracted * @return A String comprising all of the non-null matched groups concatenated together */ public static final String concatGroups(MatcherCompat matcher) { StringBuilder b = new StringBuilder(); final int numGroups = matcher.groupCount(); for (int i = 1; i <= numGroups; i++) { String s = matcher.group(i); if (s != null) { b.append(s); } } return b.toString(); }
/** * Convenience method to return only the digits and plus signs in the matching string. * * @param matcher The Matcher object from which digits and plus will be extracted * @return A String comprising all of the digits and plus in the isMatch */ public static final String digitsAndPlusOnly(MatcherCompat matcher) { StringBuilder buffer = new StringBuilder(); String matchingRegion = matcher.group(); for (int i = 0, size = matchingRegion.length(); i < size; i++) { char character = matchingRegion.charAt(i); if (character == '+' || Character.isDigit(character)) { buffer.append(character); } } return buffer.toString(); }