code: public static String extractCommonAncestor(String pattern, String absPath)
     {
        pattern = normalizePath(pattern);
        absPath = normalizePath(absPath);
  
        String[] patterEntries = pattern.split("/");
        String[] pathEntries = absPath.split("/");
  
        StringBuilder ancestor = new StringBuilder();
        int count = Math.min(pathEntries.length, patterEntries.length);
  
        for (int i = 1; i < count; i++)
        {
           if (acceptName(patterEntries[i], pathEntries[i]))
           {
              ancestor.append("/");
              ancestor.append(pathEntries[i]);
           }
           else
           {
              break;
           }
        }
  
        return ancestor.length() == 0 ? JCRPath.ROOT_PATH : ancestor.toString();
     }
docs: Returns common ancestor for paths represented by absolute path and pattern.