該場景的最佳正則表達式是什麼?匹配URL的路徑,減去文件擴展名
鑑於這種網址:
http://php.net/manual/en/function.preg-match.php
我應該如何去選擇之間的所有內容(但不包括)http://php.net
和.php
:
/manual/en/function.preg-match
這是一個Nginx配置文件。
該場景的最佳正則表達式是什麼?匹配URL的路徑,減去文件擴展名
鑑於這種網址:
http://php.net/manual/en/function.preg-match.php
我應該如何去選擇之間的所有內容(但不包括)http://php.net
和.php
:
/manual/en/function.preg-match
這是一個Nginx配置文件。
像這樣:
if (preg_match('/(?<=net).*(?=\.php)/', $subject, $regs)) {
$result = $regs[0];
}
說明:
"
(?<= # Assert that the regex below can be matched, with the match ending at this position (positive lookbehind)
net # Match the characters 「net」 literally
)
. # Match any single character that is not a line break character
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
\. # Match the character 「.」 literally
php # Match the characters 「php」 literally
)
"
試試這個:
preg_match("/net(.*)\.php$/","http://php.net/manual/en/function.preg-match.php", $matches);
echo $matches[1];
// prints /manual/en/function.preg-match
這是一般的URL匹配,您可以選擇一個URL的一部分:
if (preg_match('/\\b(?P<protocol>https?|ftp):\/\/(?P<domain>[-A-Z0-9.]+)(?P<file>\/[-A-Z0-9+&@#\/%=~_|!:,.;]*)?(?P<parameters>\\?[-A-Z0-9+&@#\/%=~_|!:,.;]*)?/i', $subject, $regs)) {
$result = $regs['file'];
//or you can append the $regs['parameters'] too
} else {
$result = "";
}
正則表達式可能不是這項工作最有效的工具。
嘗試使用parse_url()
,結合pathinfo()
:
$url = 'http://php.net/manual/en/function.preg-match.php';
$path = parse_url($url, PHP_URL_PATH);
$pathinfo = pathinfo($path);
echo $pathinfo['dirname'], '/', $pathinfo['filename'];
上面的代碼輸出:
/manual/en/function.preg-match
正則表達式之後 「淨」 一切匹配和前 「.PHP」:
$pattern = "net([a-zA-Z0-9_]*)\.php";
在上面的正則表達式中,可以找到「()」所包含的匹配字符組就是您要查找的內容。
希望它有用。
這不符合給定的例子,因爲它有一個點:'function.preg-match' – Toto
更不用說它也不匹配斜線。此外,正則表達式不是錨定的 - 這可能不會導致問題(默認情況下'*'運算符是貪婪的),但這不是一個好習慣。 – 2012-01-06 00:20:59
沒有必要使用正則表達式來剖析URL。 PHP爲此具有內置函數,pathinfo()和parse_url()。
這裏是一個正則表達式的解決方案比大多數迄今提供了更好的,如果你問我:http://regex101.com/r/nQ8rH5
/http:\/\/[^\/]+\K.*(?=\.[^.]+$)/i
簡單:
$url = "http://php.net/manual/en/function.preg-match.php";
preg_match("/http:\/\/php\.net(.+)\.php/", $url, $matches);
echo $matches[1];
$matches[0]
是完整的URL,$matches[1]
是一部分,你想。
只爲它的樂趣,這裏是尚未探索兩種方式:
substr($url, strpos($s, '/', 8), -4)
或者:基於這樣的理念
substr($s, strpos($s, '/', 8), -strlen($s) + strrpos($s, '.'))
那HTTP方案http://
和https://
最多爲8個字符,因此通常只需從第9個位置開始找到第一個斜槓。如果擴展總是.php
第一個代碼將起作用,否則另一個是必需的。
對於純的正則表達式溶液可以打破串向下這樣的:
~^(?:[^:/?#]+:)?(?://[^/?#]*)?([^?#]*)~
^
路徑部將所述第一存儲器組內(即,索引1),在該行由^
指示下方表達方式。卸下擴展名可以用pathinfo()
做到:
$parts = pathinfo($matches[1]);
echo $parts['dirname'] . '/' . $parts['filename'];
您也可以調整表達這樣的:
([^?#]*?)(?:\.[^?#]*)?(?:\?|$)
這種表達不是很最優的,但因爲它有一些回到它的跟蹤。最後,我會去的東西少定製:
$parts = pathinfo(parse_url($url, PHP_URL_PATH));
echo $parts['dirname'] . '/' . $parts['filename'];
re> |(?<=\w)/.+(?=\.\w+$)| Compile time 0.0011 milliseconds Memory allocation (code space): 32 Study time 0.0002 milliseconds Capturing subpattern count = 0 No options First char = '/' No need char Max lookbehind = 1 Subject length lower bound = 2 No set of starting bytes data> http://php.net/manual/en/function.preg-match.php Execute time 0.0007 milliseconds 0: /manual/en/function.preg-match
re> |//[^/]*(.*)\.\w+$| Compile time 0.0010 milliseconds Memory allocation (code space): 28 Study time 0.0002 milliseconds Capturing subpattern count = 1 No options First char = '/' Need char = '.' Subject length lower bound = 4 No set of starting bytes data> http://php.net/manual/en/function.preg-match.php Execute time 0.0005 milliseconds 0: //php.net/manual/en/function.preg-match.php 1: /manual/en/function.preg-match
re> |/[^/]+(.*)\.| Compile time 0.0008 milliseconds Memory allocation (code space): 23 Study time 0.0002 milliseconds Capturing subpattern count = 1 No options First char = '/' Need char = '.' Subject length lower bound = 3 No set of starting bytes data> http://php.net/manual/en/function.preg-match.php Execute time 0.0005 milliseconds 0: /php.net/manual/en/function.preg-match. 1: /manual/en/function.preg-match
re> |/[^/]+\K.*(?=\.)| Compile time 0.0009 milliseconds Memory allocation (code space): 22 Study time 0.0002 milliseconds Capturing subpattern count = 0 No options First char = '/' No need char Subject length lower bound = 2 No set of starting bytes data> http://php.net/manual/en/function.preg-match.php Execute time 0.0005 milliseconds 0: /manual/en/function.preg-match
re> |\w+\K/.*(?=\.)| Compile time 0.0009 milliseconds Memory allocation (code space): 22 Study time 0.0003 milliseconds Capturing subpattern count = 0 No options No first char Need char = '/' Subject length lower bound = 2 Starting byte set: 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z _ a b c d e f g h i j k l m n o p q r s t u v w x y z data> http://php.net/manual/en/function.preg-match.php Execute time 0.0011 milliseconds 0: /manual/en/function.preg-match
http:[\/]{2}.+?[.][^\/]+(.+)[.].+
讓我們來看看,是什麼做:
http:[\/]{2}.+?[.][^\/]
- 非捕獲組http://php.net
(.+)[.]
- 捕捉一部分,直到最後一個點出現:/manual/en/function.preg-match
[.].+
- 文件匹配擴展名如下:.php
'(?:http:[\ /] {2}。+?[。])[^ \ /] +(。+)[。] +。+ – gaussblurinc