private static RegionOrRegionName guessRegionOrRegionNameForEndpoint(String endpoint, String serviceHint) {
        if (endpoint == null) {
            return new RegionOrRegionName();
        }
        String host = null;

        try {
            host = URI.create(endpoint).getHost();
        } catch (Exception ex) {
            // Ignore errors. eg: IllegalArgumentException: Illegal character in scheme name at index 0: 127.0.0.1:1234
        }

        if (host == null) {
            host = URI.create("http://" + endpoint).getHost();
        }

        if (host == null) {
            return new RegionOrRegionName();
        }

        String regionFromInternalConfig = AwsHostNameUtils.parseRegionFromInternalConfig(host);
        if (regionFromInternalConfig != null) {
            return new RegionOrRegionName(regionFromInternalConfig);
        }

        RegionMetadata regionMetadata = RegionUtils.getRegionMetadata();

        Region regionByExplicitEndpoint = regionMetadata.tryGetRegionByExplicitEndpoint(host);
        if (regionByExplicitEndpoint != null) {
            return new RegionOrRegionName(regionByExplicitEndpoint);
        }

        String regionFromAwsPartitionPattern = AwsHostNameUtils.parseRegionFromAwsPartitionPattern(host);
        if (regionFromAwsPartitionPattern != null) {
            return new RegionOrRegionName(regionFromAwsPartitionPattern);
        }

        String serviceHintRegion = AwsHostNameUtils.parseRegionUsingServiceHint(host, serviceHint);
        if (serviceHintRegion != null) {
            return new RegionOrRegionName(serviceHintRegion);
        }

        Region regionByDnsSuffix = regionMetadata.tryGetRegionByEndpointDnsSuffix(host);
        if (regionByDnsSuffix != null) {
            return new RegionOrRegionName(regionByDnsSuffix);
        }

        String regionFromAfterServiceName = AwsHostNameUtils.parseRegionFromAfterServiceName(host, serviceHint);
        if (regionFromAfterServiceName != null) {
            return new RegionOrRegionName(regionFromAfterServiceName);
        }

        return new RegionOrRegionName();
    }
Attempt to convert the provided hostname and service hint into a region, returning null if the conversion fails.
aws-java-sdk-core/src/main/java/com/amazonaws/regions/EndpointToRegion.java