简体   繁体   English

如何仅对网址的最后一部分进行正则表达式?

[英]How to regex last part of URL only?

I want to do a regex that target the end of a url: 我想做一个针对网址结尾的正则表达式:

www.company.com/orders/ thanks www.company.com/orders/ 谢谢

If you return from email or account page order ID is populated in the end: 如果您是从电子邮件中返回或帐户页面,那么最后将填充订单ID:

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

So I only want to target the URL that ends with /thanks 所以我只想定位以/ thanks结尾的URL

This thread bring is similiar: How do I get the last segment of URL using regular expressions 该线程带来的相似之处: 如何使用正则表达式获取URL的最后一段

Had something similair .*\\/thanks\\/.+ but target incorrectly. 有点类似.*\\/thanks\\/.+但定位不正确。

EDIT: Only target URLs ending with /thanks or /thanks/ 编辑:仅以/ thanks或/ thanks /结尾的目标URL

Try with lookahead like this. 像这样先行尝试。

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

Explanation: This will match the URL only if thanks is at end of string by positive lookahead . 说明:仅当在positive lookahead的字符串末尾thanks时,才与URL匹配。

Regex101 Demo Regex101演示

Use URL object dont parse it yourself 

URL url = new URL("http://stackoverflow.com/questions/36616915/how-to-regex-last-part-of-url-only");
URLDecoder.decode(url.getPath(), "utf-8");
url.getPath();
url.getContent();
url.getPort();
url.getContent();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM