2011-11-29 54 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

相关问题