简体   繁体   中英

regex to exclude filename from url

I require a regex that will exclude file name from URL.

Input: 'something.something.com/som/som/som/abcd.html'

Should return: 'something.something.com/som/som/som/'

I created this regex:

((?:[a-z0-9\\.\\_]+)(?:\\/)(?:.*?)(?:\\/))(?:[a-z0-9]+)(?:\\.)(?:[a-z0-9]+)

I tried whatever I can and it is giving correct output (tested at regex101.com [PCRE]).

Do I need to add/delete anything else? Any more suggestions? And don't suggest to use parse_url() , it is useless if URL don't have 'http://'.

Using PHP:

$dir = dirname("something.something.com/som/som/som/abcd.html");

Using regex:

$dir = preg_replace("|[^/]+\.[^/]+$|", "$1", $dir);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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