/* package whatever; // don't place package name! */
import java.util.*;
import java.util.regex.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
Pattern p = Pattern.compile("^(dog)(?:.+(cat))?");
for (String s
: new String[] {"dog, cat",
"dog, dog, cat",
"dog, dog, dog",
"dog, dog, cat, blah"}) { Matcher m = p.matcher(s);
if (m.find()) {
System.
out.
println(m.
group(1)+" "+m.
group(2)); }
}
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS51dGlsLnJlZ2V4Lio7CmltcG9ydCBqYXZhLmxhbmcuKjsKaW1wb3J0IGphdmEuaW8uKjsKCi8qIE5hbWUgb2YgdGhlIGNsYXNzIGhhcyB0byBiZSAiTWFpbiIgb25seSBpZiB0aGUgY2xhc3MgaXMgcHVibGljLiAqLwpjbGFzcyBJZGVvbmUKewoJcHVibGljIHN0YXRpYyB2b2lkIG1haW4gKFN0cmluZ1tdIGFyZ3MpIHRocm93cyBqYXZhLmxhbmcuRXhjZXB0aW9uCgl7CgkJUGF0dGVybiBwID0gUGF0dGVybi5jb21waWxlKCJeKGRvZykoPzouKyhjYXQpKT8iKTsKCQlmb3IgKFN0cmluZyBzIDogbmV3IFN0cmluZ1tdIHsiZG9nLCBjYXQiLCAiZG9nLCBkb2csIGNhdCIsICJkb2csIGRvZywgZG9nIiwgImRvZywgZG9nLCBjYXQsIGJsYWgifSkgewoJCQlNYXRjaGVyIG0gPSBwLm1hdGNoZXIocyk7CgkJCWlmIChtLmZpbmQoKSkgewoJCQkJU3lzdGVtLm91dC5wcmludGxuKG0uZ3JvdXAoMSkrIiAiK20uZ3JvdXAoMikpOwoJCQl9CgkJfQoJfQp9