我必须从中删除域的网址:http ://www.espn.com/watch/?gameId=1234&league=nfl&lang=es&profile=sportscenter_v1
您是否知道一种更好的方法来实现这一目标,而无需使用以下内容:
def 示例= decodeUrl.replace(" http://www.espn.com ", "" )
谢谢
我必须从中删除域的网址:http ://www.espn.com/watch/?gameId=1234&league=nfl&lang=es&profile=sportscenter_v1
您是否知道一种更好的方法来实现这一目标,而无需使用以下内容:
def 示例= decodeUrl.replace(" http://www.espn.com ", "" )
谢谢
使用 java.net.URI 类。如果您从 url 字符串创建 URI,您将可以访问地址、行、路径、查询、方案、主机端口等的所有组件。 https://docs.oracle.com/javase/7/docs/api/java /net/URI.html
URI uri = new URI("http://www.espn.com/watch/?gameId=1234&league=nfl&lang=es&profile=sportscenter_v1")
println uri.path +"?"+ uri.query
你可以这样写,这样域就无关紧要了。
所以这里我们使用模式匹配
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 分享的答案是最合乎逻辑的,但这也是一种方法
如果您的域总是“.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