Match the path of a URL, minus the filename extension
Posted By: Anonymous
What would be the best regular expression for this scenario?
Given this URL:
http://php.net/manual/en/function.preg-match.php
How should I go about selecting everything between (but not including) http://php.net
and .php
:
/manual/en/function.preg-match
This is for an Nginx configuration file.
Solution
Like this:
if (preg_match('/(?<=net).*(?=.php)/', $subject, $regs)) {
$result = $regs[0];
}
Explanation:
"
(?<= # 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
)
"
Answered By: Anonymous
Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.