Regex in Notepad++ : How to get last part of URLs?

瘦欲@ 提交于 2019-12-19 13:46:06

问题


I have some URL like this:

http://www.mywebsite.com/at/hello-everybody-mn0000000003
http://www.mywebsite.com/am/I-dont-care-mw0000322930
http://www.mywebsite.com/ccc/mood/I-love-you-d5207

How to get the last part which removed the last word?

hello-everybody
I-dont-care
I-love-you

回答1:


You could use the below lookahead based regex.

[^\/]+?(?=-[^-\/]+$)

DEMO




回答2:


hope it can help you!

$str = 'http://www.mywebsite.com/at/hello-everybody-mn0000000003';
$str = preg_replace('@http://www.mywebsite.com/(.*)/@','',$str);
$str = substr($str,0,strrpos($str,'-'));



回答3:


Unfortunately, this is the furthest I can get you:

(?!\/)(?:.(?!\/))+(?=-)

This is what it would match:

http://www.mywebsite.com/at/hello-everybody-mn0000000003   Matches: hello-everybody
http://www.mywebsite.com/am/I-dont-care-mw0000322930       Matches: I-dont-care
http://www.mywebsite.com/ccc/mood/I-love-you-d5207         Matches: I-love-you



回答4:


Try this RegEx: /[\w-]+(?=-)/.

Example: https://regex101.com/r/gX3sD2/1



来源:https://stackoverflow.com/questions/27595856/regex-in-notepad-how-to-get-last-part-of-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!