【问题标题】:Java : how to get text between "http://" and first following "/" occurence ? And after first "/" occurence?Java:如何在“http://”和第一个“/”出现之间获取文本?在第一次“/”出现之后?
【发布时间】:2014-03-08 09:57:06
【问题描述】:

我还是 Java 中正则表达式、“regex”等的新手。

如果我有这样的网址:“http://somedomain.someextention/somefolder/.../someotherfolder/somepage

什么是最简单的获取方法:

  • “somedomain.someextention”?
  • “somefolder/.../someotherfolder/somepage”?
  • “某页”?

谢谢!

【问题讨论】:

标签: java android regex


【解决方案1】:

您不必(也可能不应该)在此处使用正则表达式。而是使用定义的类来处理这样的事情。您可以使用例如URLURIFile 之类的类

String address = "http://somedomain.someextention/somefolder/.../someotherfolder/somepage";

URL url = new URL(address);
File file = new File(url.getPath());

System.out.println(url.getHost());
System.out.println(url.getPath());
System.out.println(file.getName());

输出:

somedomain.someextention
/somefolder/.../someotherfolder/somepage
somepage

现在您可能需要在资源路径的开头删除 /。如果资源以/ 开头,您可以在此处使用substring(1)


但如果你真的必须使用正则表达式,你可以试试

^https?://([^/]+)/(.*/([^/]+))$

现在

  • 组 1 将包含主机名,
  • 组 2 将包含资源路径
  • 组 3 将包含资源名称

【讨论】:

    【解决方案2】:

    获取这些组件的最佳方法是使用URI 类;例如

        URI uri = new URI(str);
        String domain = uri.getHost();
        String path = uri.getPath();
        int pos = path.lastIndex("/");
        ...
        // or use File to parse the path string.
    

    可以对原始 url 字符串使用正则表达式,但存在无法正确处理 URL 中可能存在的所有可变性的风险。 (提示:@Pchenko 提供的正则表达式没有 :-))而且您肯定需要使用解码器来处理可能的百分比编码。

    【讨论】:

      【解决方案3】:

      这不是正则表达式或 URI 使用,而是简单的子字符串代码作为练习材料。缺少一些极端情况格式验证。

      int lastDelim = str.lastIndexOf('/);
      if (lastDelim<0) throw new IllegalArgumentException("Invalid url");
      int startIdx = str.indexOf("//");
      startIdx = startIdx<0 ? 0 : startIdx+2;
      int pathDelim = str.indexOf('/', startIdx);
      String domain = str.substring(startIdx, pathDelim);
      String path = str.substring(pathDelim+1, lastDelim);
      String page = str.substring(lastDelim+1);
      

      【讨论】:

        【解决方案4】:

        如果您想使用正则表达式来解码 URL,而不是使用 URI 类,如前面的答案中所述,下面的链接提供了一个很好的正则表达式教程,它还解释了解码示例 URL。你可以在那里学习并尝试一下。

        http://www.beedub.com/book/2nd/regexp.doc.html

        【讨论】:

          【解决方案5】:

          它不是正则表达式,也不是可扩展的,但它可以工作:

          public class SomeClass
          {
              public static void main(String[] args)
              {
          
                  SomeClass sclass = new SomeClass();
                  String[] string = 
                      sclass.parseURL("http://somedomain.someextention/somefolder/.../someotherfolder/somepage");
          
                  System.out.println(string[0]);
                  System.out.println(string[1]);
                  System.out.println(string[2]);
              }
          
              private String[] parseURL(String url)
              {
                  String part1 = url.substring("http://".length(), url.indexOf("/", "http://".length()));
          
                  String part2 = url.substring("http://".length() + part1.length() + 1, url.lastIndexOf("/"));
          
                  String part3 = url = url.substring(url.lastIndexOf("/") + 1);
          
                  return new String[] { part1, part2, part3 };
              }
          }
          

          输出:

          somedomain.someextention
          somefolder/.../someotherfolder
          somepage
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-10-13
            • 1970-01-01
            相关资源
            最近更新 更多