Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. // Compile and use regular expression
  2. Pattern pattern = Pattern.compile(patternStr);
  3. Matcher matcher = pattern.matcher(inputStr);
  4. boolean matchFound = matcher.find();
  5.  
  6. if (matchFound) {
  7. // Get all groups for this match
  8. for (int i=0; i<=matcher.groupCount(); i++) {
  9. String groupStr = matcher.group(i);
  10. System.out.println(groupStr);
  11. }
  12. }
  13.  
  14. // Compile and use regular expression
  15. RegExp regExp = RegExp.compile(patternStr);
  16. MatchResult matcher = regExp.exec(inputStr);
  17. boolean matchFound = matcher != null; // equivalent to regExp.test(inputStr);
  18.  
  19. if (matchFound) {
  20. // Get all groups for this match
  21. for (int i = 0; i < matcher.getGroupCount(); i++) {
  22. String groupStr = matcher.getGroup(i);
  23. System.out.println(groupStr);
  24. }
  25. }
  26.  
  27. private ArrayList<String> getMatches(String input, String pattern) {
  28. ArrayList<String> matches = new ArrayList<String>();
  29. RegExp regExp = RegExp.compile(pattern, "g");
  30. for (MatchResult matcher = regExp.exec(input); matcher != null; matcher = regExp.exec(input)) {
  31. matches.add(matcher.getGroup(0));
  32. }
  33. return matches;
  34. }
  35.  
  36. ArrayList<String> matches = getMatches(someInputStr, "\$\{[A-Za-z_0-9]+\}");
  37. for (int i = 0; i < matches.size(); i++) {
  38. String match = matches.get(i);
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement