代码之家  ›  专栏  ›  技术社区  ›  rasilvap

从url中删除域的最佳方法

  •  1
  • rasilvap  · 技术社区  · 6 年前

    我必须从此中删除域的url: http://www.espn.com/watch/?gameId=1234&league=nfl&lang=es&profile=sportscenter_v1

    你知道一个更好的方法来实现这一点,而不用像:

    def example=decodeUrl.replace(“ http://www.espn.com ", "" )

    3 回复  |  直到 6 年前
        1
  •  0
  •   yeugeniuss    6 年前

    使用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
    
        2
  •  0
  •   Nitin Dhomse    6 年前

    那你可以试试下面的

         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
    
        3
  •  0
  •   Gaurav Khurana    6 年前

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

    所以我们用的是 模式匹配

    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

    你可能不得不使用 因此,如果只有www.espn.com没有任何参数来处理这种情况

    为了覆盖url的所有可能场景,您可以引用 link