fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.regex.*;
  5. import java.lang.*;
  6. import java.io.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. String input = "some text key 12, 32, 311 ,465 and 345. some other text dog 612,\n12, 32, 9 and 10. some text key 1, 2. key 1, 2 dog 3, 4 key 5, 6. key is dog 23, 45. key 4";
  14. Pattern p = Pattern.compile("(?:key(?>\\s+and\\s+|[\\s,]+)|(?!^)\\G(?>\\s+and\\s+|[\\s,]+))(\\d+)");
  15. Matcher m = p.matcher(input);
  16. List<String> numbers = new ArrayList<>();
  17. while (m.find()) {
  18. numbers.add(m.group(1));
  19. }
  20. System.out.println(numbers);
  21. }
  22. }
Success #stdin #stdout 0.11s 320256KB
stdin
Standard input is empty
stdout
[12, 32, 311, 465, 345, 1, 2, 1, 2, 5, 6, 4]