问题
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