2011-11-29 95 views
10

該場景的最佳正則表達式是什麼?匹配URL的路徑,減去文件擴展名

鑑於這種網址:

http://php.net/manual/en/function.preg-match.php 

我應該如何去選擇之間的所有內容(但不包括)http://php.net.php

/manual/en/function.preg-match 

這是一個Nginx配置文件。

+0

'(?:http:[\ /] {2}。+?[。])[^ \ /] +(。+)[。] +。+ – gaussblurinc

回答

7

像這樣:

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 
) 
" 
+0

感謝FailedDev的詳細解釋,這完美的工作 – silkAdmin

+3

'/ net(。*)\。php /'',更簡單,更短(可能更好的表現)版本的同一個表達式。 (我更喜歡錶達式,沒有浪費的不必要的外觀。) – Qtax

2

試試這個:

preg_match("/net(.*)\.php$/","http://php.net/manual/en/function.preg-match.php", $matches); 
echo $matches[1]; 
// prints /manual/en/function.preg-match 
0

這是一般的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 = ""; 
} 
19

正則表達式可能不是這項工作最有效的工具。

嘗試使用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
+0

感謝鳳凰,這也工作,但我一直在尋找一個reg解決方案。 – silkAdmin

+1

@silkAdmin這很好奇;爲什麼解決方案必須是正則表達式? – 2012-01-05 16:14:18

+1

對不起,我不應該使用令人困惑的PHP標記,我需要一個Nginx配置文件的正則表達式,其中您的解決方案不是一個選項 – silkAdmin

-1

正則表達式之後 「淨」 一切匹配和前 「.PHP」:

$pattern = "net([a-zA-Z0-9_]*)\.php"; 

在上面的正則表達式中,可以找到「()」所包含的匹配字符組就是您要查找的內容。

希望它有用。

+2

這不符合給定的例子,因爲它有一個點:'function.preg-match' – Toto

+0

更不用說它也不匹配斜線。此外,正則表達式不是錨定的 - 這可能不會導致問題(默認情況下'*'運算符是貪婪的),但這不是一個好習慣。 – 2012-01-06 00:20:59

2

沒有必要使用正則表達式來剖析URL。 PHP爲此具有內置函數,pathinfo()parse_url()

0

這裏是一個正則表達式的解決方案比大多數迄今提供了更好的,如果你問我:http://regex101.com/r/nQ8rH5

 
/http:\/\/[^\/]+\K.*(?=\.[^.]+$)/i 
0

簡單:

$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]是一部分,你想。

見自己:http://codepad.viper-7.com/hHmwI2

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']; 
0

|(?< = \ W)/.+(?= \ \ w + $)|

  • 選擇一切從第一行文字 '/' 用字(\ w)的字符
  • 背後
  • 看前面,直到後面前瞻
    • 文字 ''通過
    • 一個或多個字(\ w)的字符
    • 結束$
 
    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 

之前所附| // [^ /] *(。*)\ \ W + $ |

  • 發現兩個文字 '//',然後什麼,但文字 '/'
  • 選擇一切,直到
  • 找到文字 ''其次才字\ W底$
 
    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 

前字符|/[^ /] +(。*)\。|

  • 找到文字「/」後面至少1個或多個非文字「/」
  • 積極的選擇前的最後文字的一切「」
 
    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 

|/[^ /] + \ K *(= \?)。|

  • 找到文字「/」後面至少1個或多個非文字「/」
  • 復位選擇開始\ķ
  • 之前激進的選擇一切
  • 向前看最後文字「」
 
    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 

| \ W + \ķ/.*(= \?)|

  • 之前找到一個或多個字(\ w)的字符的文字 '/'
  • 復位選擇開始\ķ
  • 選擇文字 '/',然後
  • 事情之前
  • 向前看最後文字'。'
 
    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 
-1

http:[\/]{2}.+?[.][^\/]+(.+)[.].+

讓我們來看看,是什麼做:

http:[\/]{2}.+?[.][^\/] - 非捕獲組http://php.net

(.+)[.] - 捕捉一部分,直到最後一個點出現:/manual/en/function.preg-match

[.].+ - 文件匹配擴展名如下:.php

相關問題