如何仅正则表达式 URL 的最后一部分?

How to regex last part of URL only?

我想做一个以 url 结尾为目标的正则表达式:

www.company.com/orders/谢谢

如果您 return 来自电子邮件或帐户页面的订单 ID 填充在末尾:

www.company.com/orders/thanks/1sfasd523425

所以我只想定位以 结尾的 URL/thanks

本帖带同:How do I get the last segment of URL using regular expressions

有类似的东西.*\/thanks\/.+但目标不正确。

编辑:仅目标 URLs 以 /thanks 或 /thanks/

结尾

尝试像这样向前看。

正则表达式: .+(?=\/thanks$).+

说明: 只有当 thanks 位于字符串末尾 positive lookahead 时,才会匹配 URL。

Regex101 Demo

Use URL object dont parse it yourself 

URL url = new URL("");
URLDecoder.decode(url.getPath(), "utf-8");
url.getPath();
url.getContent();
url.getPort();
url.getContent();