Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get url from a string groovy

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

like image 940
clever_bassi Avatar asked Feb 12 '26 05:02

clever_bassi


1 Answers

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/'
like image 112
Jeff Scott Brown Avatar answered Feb 15 '26 23:02

Jeff Scott Brown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!