On 8 April 2014 12:14, manoj mokashi <manojmoka...@gmail.com> wrote: > hi, > > do we have something like the below already ? > if not, would like to contribute to commons, maybe StringUtils ?
Just curious - what is the use case for this? What does the method return if there are no matches? > /** > * 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 ); > } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org