/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
List
<String
> strs
= Arrays.
asList("TEST1 AVOID TEST2 2016 TEST3 TEST4",
"TEST1 TEST2 2016 TEST3 TEST4",
"TEST1 TEST2 2016 TEST3 TEST4 AVOID"); String pat
= "^(?!.*\\bAVOID\\b).*?\\b(\\d{4})\\b"; Matcher m = Pattern.compile(pat).matcher(str);
if (m.find()) {
System.
out.
println(m.
group(1)); } else {
System.
out.
println("No match for " + str
+ " :("); }
}
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CmltcG9ydCBqYXZhLnV0aWwucmVnZXguKjsKLyogTmFtZSBvZiB0aGUgY2xhc3MgaGFzIHRvIGJlICJNYWluIiBvbmx5IGlmIHRoZSBjbGFzcyBpcyBwdWJsaWMuICovCmNsYXNzIElkZW9uZQp7CglwdWJsaWMgc3RhdGljIHZvaWQgbWFpbiAoU3RyaW5nW10gYXJncykgdGhyb3dzIGphdmEubGFuZy5FeGNlcHRpb24KCXsKCQlMaXN0PFN0cmluZz4gc3RycyA9IEFycmF5cy5hc0xpc3QoIlRFU1QxIEFWT0lEIFRFU1QyIDIwMTYgVEVTVDMgVEVTVDQiLCAiVEVTVDEgVEVTVDIgMjAxNiBURVNUMyBURVNUNCIsICJURVNUMSBURVNUMiAyMDE2IFRFU1QzIFRFU1Q0IEFWT0lEIik7CgkJU3RyaW5nIHBhdCA9ICJeKD8hLipcXGJBVk9JRFxcYikuKj9cXGIoXFxkezR9KVxcYiI7CgkJZm9yIChTdHJpbmcgc3RyIDogc3RycykgewoJCQlNYXRjaGVyIG0gPSBQYXR0ZXJuLmNvbXBpbGUocGF0KS5tYXRjaGVyKHN0cik7CQoJCQlpZiAobS5maW5kKCkpIHsKCQkJCVN5c3RlbS5vdXQucHJpbnRsbihtLmdyb3VwKDEpKTsKCQkJfSBlbHNlIHsKCQkJCVN5c3RlbS5vdXQucHJpbnRsbigiTm8gbWF0Y2ggZm9yICIgKyBzdHIgKyAiIDooIik7CgkJCX0KCQl9Cgl9Cn0=