I am working with a grails app. I need to extract only part of the url up to .com (or gov, edu, mil, org, net, etc.) from a string.
For example:
Input: https://stackoverflow.com/questions?=34354#es4 Output: https://stackoverflow.com/
Input: https://code.google.com/p/crawler4j/issues/detail?id=174 Output: https://code.google.com/
Can anyone suggest how it can be done? Also, if it can be done, I need to change https to http in the resulting string. Please help. Thanks.
Edit: I apologize to all the downvoters that I did not include the thing that I tried. This is what i tried:
URL url = new URL(website);
String webUrl = url.getprotocol()+"://"+url.getAuthority()
But I got the following error: MissingPropertyException occurred when processing request: [POST] /mypackage/resource/crawl
Something like this satisfies the 2 examples given:
def url = new URL('http://stackoverflow.com/questions?=34354#es4')
def result = 'http://' + url.host +'/'
assert result == 'http://stackoverflow.com/'
def url2 = new URL('https://code.google.com/p/crawler4j/issues/detail?id=174')
def result2 = 'http://' + url2.host +'/'
assert result2 == 'http://code.google.com/'
EDIT:
Of course you can abbreviate the concatenation with something like this:
def url = new URL('http://stackoverflow.com/questions?=34354#es4')
def result = "http://${url.host}/"
assert result == 'http://stackoverflow.com/'
def url2 = new URL('https://code.google.com/p/crawler4j/issues/detail?id=174')
def result2 = "http://${url2.host}/"
assert result2 == 'http://code.google.com/'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With