hi,

do we have something like the below already ?
if not, would like to contribute to commons, maybe StringUtils ?

/**
 * Matches a string to a regex and returns the matches as String[][],
 * each row is a match, each column a group
 * @param source : string to be matched
 * @param regex : regex to be matched
 * @return : String[][] containing matches
 */
public static String[][] getMatches( String source, String regex){
Pattern pattern = Pattern.compile( regex);
Matcher m = pattern.matcher( source);
List<String[]> matches = new ArrayList();
int numGroups = m.groupCount();
while( m.find()){
                        // group 0 is the full match
String[] groups = new String[numGroups+1];
for( int i=0;i<=numGroups; i++){
groups[i] = m.group(i);
}
matches.add( groups);
}
String[][] arrMatches = new String[matches.size()][numGroups];
return matches.toArray( arrMatches );
}

Reply via email to