Skip to content

Instantly share code, notes, and snippets.

@turboBasic
Last active January 2, 2022 16:07
Show Gist options
  • Save turboBasic/bf7ae6bf65e02a5f9a4edc6d92e0e6c1 to your computer and use it in GitHub Desktop.
Save turboBasic/bf7ae6bf65e02a5f9a4edc6d92e0e6c1 to your computer and use it in GitHub Desktop.
getHostFromUrl() #groovy
String getHostFromUrl(String url) {
url
.replaceAll('^[a-z]+://', '')
.replaceAll('(/.*)?$', '')
.replaceAll('(:[0-9]+)?$', '')
}
def getHostFromUrlSpec() {
expect:
getHostFromUrl('https://example.com:9999/path.ext') == 'example.com'
getHostFromUrl('https://example.com/path.ext') == 'example.com'
getHostFromUrl('https://example.com') == 'example.com'
getHostFromUrl('ssh://example.com') == 'example.com'
getHostFromUrl('file:///usr/local/bin/sh') == ''
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment