Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. private static final String LIST_REGEX = "^(\d+)(,\d+)*$";
  2. private static final Pattern LIST_PATTERN = Pattern.compile(LIST_REGEX);
  3.  
  4. public static void main(String[] args) {
  5. final String list = "1,2,3,4,5";
  6. final Matcher matcher = LIST_PATTERN.matcher(list);
  7. System.out.println(matcher.matches());
  8. for (int i = 0, n = matcher.groupCount(); i < n; i++) {
  9. System.out.println(i + "t" + matcher.group(i));
  10. }
  11. }
  12.  
  13. true
  14. 0 1,2,3,4,5
  15. 1 1
  16.  
  17. private static final String LIST_REGEX = "^\[?(\d+(?:,\d+)*)\]?$";
  18. private static final Pattern LIST_PATTERN = Pattern.compile(LIST_REGEX);
  19.  
  20. public static void main(String[] args) {
  21. final String list = "[1,2,3,4,5]";
  22. final Matcher matcher = LIST_PATTERN.matcher(list);
  23.  
  24. matcher.find();
  25. int i = 0;
  26.  
  27. String[] vals = matcher.group(1).split(",");
  28.  
  29. System.out.println(matcher.matches());
  30. System.out.println(i + "t" + matcher.group(1));
  31.  
  32. for (String x : vals) {
  33. i++;
  34. System.out.println(i + "t" + x);
  35. }
  36. }
  37.  
  38. true
  39. 0 1,2,3,4,5
  40. 1 1
  41. 2 2
  42. 3 3
  43. 4 4
  44. 5 5
  45.  
  46. for (String segment : "1,2,3,4,5".split(","))
  47. System.out.println(segment);
  48.  
  49. Pattern pattern = Pattern.compile("(\d),?");
  50. for (Matcher m = pattern.matcher("1,2,3,4,5");; m.find())
  51. m.group(1);
  52.  
  53. for (String segment : "!!!!![1,2,3,4,5] //"
  54. .replaceFirst("^\D*(\d(?:,\d+)*)\D*$", "$1")
  55. .split(","))
  56. System.out.println(segment);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement