3

How do I get the last part of the a URL using a regex, here is my URL, I want the segmeent between the last forward slash and the #

http://mycompany.com/test/id/1234#this

So I only want to get 1234.

I have the following but is not removing the '#this'

".*/(.*)(#|$)",

I need this while indexing data so don't want to use the URL class.

4

2 回答 2

5

只需使用URI

final URI uri = URI.create(yourInput);
final String path = uri.getPath();
path.substring(path.lastIndexOf('/') + 1); // will return what you want

还将处理带有查询字符串等的 URI。无论如何,当必须从 URL(这一个 URI)中提取任何部分时,使用正则表达式不是你想要的:URI可以为你处理这一切,在很多成本更低——因为它有一个专用的解析器。

演示代码还使用 GuavaOptional来检测 URI 没有路径组件的情况:

public static void main(final String... args) {
    final String url = "http://mycompany.com/test/id/1234#this";
    final URI uri = URI.create(url);
    final String path = Optional.fromNullable(uri.getPath()).or("/");
    System.out.println(path.substring(path.lastIndexOf('/') + 1));
}
于 2013-07-24T15:49:25.390 回答
3

how about:

".*/([^/#]*)(#.*|$)"
于 2013-07-24T15:45:37.720 回答