/* package whatever; // don't place package name! */
importjava.util.*;
importjava.util.regex.*;
importjava.lang.*;
importjava.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
publicstaticvoid main (String[] args)throws java.lang.Exception
{
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";
Pattern p = Pattern.compile("(?:key(?>\\s+and\\s+|[\\s,]+)|(?!^)\\G(?>\\s+and\\s+|[\\s,]+))(\\d+)");