Util { public static String extractDockerContainerIdFromString(String s) { if (s == null || !s.contains("docker")) { return null; } String haystack = s.trim().substring(s.indexOf("docker") + "docker".length()); Matcher m = Pattern.compile(".*?([0-9a-fA-F]{8,}).*?").matcher(haystack); if (m.matches()) { return m.group(1); } return null; }  static String toJson(Object obj); static T fromJson(String json, Class<T> clazz); static T fromJson(Reader in, TypeReference<T> typeReference); static String urlEncode(String str); static String urlEncodeQuery(String str); static StringBuilder getContextUrlString(String baseUrl, String context); static void ensureEndsWithSlash(StringBuilder str); static long copyStream(InputStream input, OutputStream output, boolean closeStreams); static String slurpStream(InputStream input, String encoding); static String read(File file); static void write(String string, File file); static String base64Encode(String plainTextString); static String getMachineId(); static String getContainerId(); static String extractDockerContainerIdFromString(String s); static Properties extractPropertiesFromExpression(String expression);  }
@Test public void testExtractHashFromString() { Assert.assertNull(Util.extractDockerContainerIdFromString(null)); Assert.assertNull(Util.extractDockerContainerIdFromString("")); Assert.assertNull(Util.extractDockerContainerIdFromString("\n")); Assert.assertNull(Util.extractDockerContainerIdFromString("This has no hash")); Assert.assertNull(Util.extractDockerContainerIdFromString("Missing the word dock?r 1234567890abcdefABCDEF")); String l; String hash; l = "11:memory:/docker/99cb4a5d8c7a8a29d01dfcbb7c2ba210bad5470cc7a86474945441361a37513a\n"; hash = "99cb4a5d8c7a8a29d01dfcbb7c2ba210bad5470cc7a86474945441361a37513a"; Assert.assertEquals(hash, Util.extractDockerContainerIdFromString(l)); l = "11:freezer:/system.slice/docker-c4a970c28c9d277373b5d1458679ac17c10db8538dd081072a95682b4396674f.scope\n"; hash = "c4a970c28c9d277373b5d1458679ac17c10db8538dd081072a95682b4396674f"; Assert.assertEquals(hash, Util.extractDockerContainerIdFromString(l)); l = "9876543210fedcba blah blah docker blah blah-1234567890abcdefABCDEF.yadda fedcba9876543210"; hash = "1234567890abcdefABCDEF"; Assert.assertEquals(hash, Util.extractDockerContainerIdFromString(l)); }