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