该场景的最佳正则表达式是什么?匹配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