简体   繁体   中英

How to extract a filename using regex from an absolute path

I am trying to extract just the filename from the following line:

"GET /mapitzend/public/components/font-awesome/css/font-awesome.css HTTP/1.1" 200 26651

In this case the expected result should be "font-awesome.css" I have gotten this far with my regex:

\"(.[^\"]+)\s+HTTP

eith the regex above it returns: "GET /mapitzend/public/components/font-awesome/css/font-awesome.css HTTP

What am I missing here..

Try this regex:

(?<=\\/)[^\\s\\/]*(?=\\s*HTTP)

Click for Demo

Explanation:

  • (?<=\\/) - positive lookbehind to find the position immediately preceded by a /
  • [^\\s\\/]* - matches 0+ occurrences of any character which is neither a whitespace character nor a /
  • (?=\\s*HTTP) - Positive lookahead to match until the position which is immediately followed by 0+ whitespaces followed by HTTP .

While extracting the file name using regex is possible, it's easier for start to extract the path using regex and then get the filename using basename() or pathinfo() :

$line = '"GET /mapitzend/public/components/font-awesome/css/font-awesome.css HTTP/1.1" 200 26651';

$m = array();
if (preg_match('/GET (.*) HTTP/', $line, $m)) {
    $filename = basename($m[1]);
}

Using only regex :

if (preg_match('#GET /(.*/)*(.*) HTTP#', $line, $m)) {
    $filename = $m[2];
}

Using properties of a filename which has certain extension like .css , .html you can use following regex.

Regex: [^\\/]+?\\.[az]+

Explanation:

[^\\/]+? matches a string whose preceding character was / .

\\.[az]+ matches the extension making sure it's a file.

Regex101 Demo

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