Groovy 从url中删除域的最佳方法

Groovy 从url中删除域的最佳方法,groovy,Groovy,我必须从以下位置删除域的url: 您是否知道一种更好的方法来实现这一目标,而无需使用以下方法: def example=decodeUrl.replace(“,”) 如果你的域名总是“.com”,谢谢 那么你可以试试下面的方法 def url = "http://www.espn.com/watch/?gameId=1234&league=nfl&lang=es&profile=sportscenter_v1" /*The split function

我必须从以下位置删除域的url:

您是否知道一种更好的方法来实现这一目标,而无需使用以下方法:

def example=decodeUrl.replace(“,”)

如果你的域名总是“.com”,谢谢 那么你可以试试下面的方法

     def url = "http://www.espn.com/watch/?gameId=1234&league=nfl&lang=es&profile=sportscenter_v1"
     /*The split function will spit the give string with a given sub string and store the splitted result in array of parts so here we are picking the second part having index 1*/
     def splitedUrl = url.split(".com")[1]
     println splitedUrl

使用java.net.URI类。若您从url字符串创建URI,那个么您将可以访问地址、行、路径、查询、方案、主机端口等的所有组件。


你可以这样写,这样域就不重要了

因此,我们在这里使用模式匹配

def uri ="http://www.espn.com/watch/?gameId=1234&league=nfl&lang=es&profile=sportscenter_v1"
def parameters= uri=~ "https?://.*?/(.*)"
log.info parameters[0][1]
到目前为止,编写的模式还可以处理http和https

您可能必须使用try catch,以防只有www.espn.com没有任何参数来处理这种情况

为了涵盖url的所有可能情况,您可以参考以下内容


尽管Yeugeniuss分享的答案是最符合逻辑的,但这也是实现这一点的方法之一

虽然这段代码可以回答这个问题,但它缺乏解释。请考虑添加文本来解释它所做的,以及它为什么回答所提出的问题。
def uri ="http://www.espn.com/watch/?gameId=1234&league=nfl&lang=es&profile=sportscenter_v1"
def parameters= uri=~ "https?://.*?/(.*)"
log.info parameters[0][1]