이 시나리오에 가장 적합한 정규식은 무엇인가요?
이 URL이 주어집니다:
http://php.net/manual/en/function.preg-match.php
00과
.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
)
"
이 일반 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 = "";
}