Posted By

aadsm on 07/28/09


Tagged

String regexp matches


Versions (?)

Missing getMatches function


 / Published in: Java
 

  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4. // Java -has- issues
  5. public String[] getMatches( String string, String regexp )
  6. {
  7. String[] matches = new String[0];
  8. Pattern pattern = Pattern.compile(regexp);
  9. Matcher matcher = pattern.matcher(string);
  10. boolean matchFound = matcher.find();
  11.  
  12. if( matchFound )
  13. {
  14. matches = new String[matcher.groupCount()];
  15. for( int i = 0; i <= matcher.groupCount(); i++ )
  16. {
  17. matches[i] = matcher.group(i);
  18. }
  19. }
  20.  
  21. return matches;
  22. }

Report this snippet  

You need to login to post a comment.