code: public static String parseRegion(final String host,
                                           final String serviceHint) {
  
          if (host == null) {
              throw new IllegalArgumentException("hostname cannot be null");
          }
          String regionNameInInternalConfig = parseRegionNameByInternalConfig(host);
          if (regionNameInInternalConfig != null) {
              return regionNameInInternalConfig;
          }
  
          if (host.endsWith(".amazonaws.com")) {
              int index = host.length() - ".amazonaws.com".length();
              return parseStandardRegionName(host.substring(0, index));
          }
  
          if (serviceHint != null) {
              if (serviceHint.equals("cloudsearch")
                      && !host.startsWith("cloudsearch.")) {
  
                  // CloudSearch domains use the nonstandard domain format
                  // [domain].[region].cloudsearch.[suffix].
  
                  Matcher matcher = EXTENDED_CLOUDSEARCH_ENDPOINT_PATTERN
                          .matcher(host);
  
                  if (matcher.matches()) {
                      return matcher.group(1);
                  }
              }
  
              // If we have a service hint, look for 'service.[region]' or
              // 'service-[region]' in the endpoint's hostname.
              Pattern pattern = Pattern.compile(
                  "^(?:.+\\.)?"
                  + Pattern.quote(serviceHint)
                  + "[.-]([a-z0-9-]+)\\."
              );
  
              Matcher matcher = pattern.matcher(host);
              if (matcher.find()) {
                  return matcher.group(1);
              }
          }
  
          // Endpoint is non-standard
          return null;
      }
docs: Attempts to parse the region name from an endpoint based on conventions
  about the endpoint format.
  
  @param host         the hostname to parse
  @param serviceHint  an optional hint about the service for the endpoint
  @return             the region parsed from the hostname, or
  null if no region information could be found.