是否有可能使正则表达式中的字符无效
Is it possible to make ineffective a character in regex
我正在尝试编写 wordpress 漂亮的永久链接正则表达式。
我有以下网址。我需要 2 场比赛,
1st : get/ 之前 / 和 / 之间的最后一个词
第二个:以 get/ 开头的字符串
Url的可能是这样的
http://localhost/akasia/yacht-technical-services/yacht-crew/get/gulets/for/sale/
这里我需要"yacht-crew"和"get/gulets/for/sale/"
http://localhost/akasia/testimonials/get/motoryachts/for/sale/
这里我需要"testimonials"和get/motoryachts/for/sale/
http://localhost/akasia/may/be/lots/of/seperator/but/ineed/last/get/ships/for/rent/
这里我需要"last"和get/ships/for/rent/
$url1 = 'somepage/get/gulets/for/sale'; // I need somepage and get/gulets/for/sale
$url2 = 'somepage/subpage/get/motoryachts/for/rent'; // I need subpage and get/motoryachts/for/rent
$url3 = 'may/be/unlimited/directories/but/i/need/here/get/ships/for/sale'; // I need here and get/ships/for/sale
$url4 = 'services/get/gulets/for/sale'; // I need services and get/gulets/for/sale
$regex = '([^\/]+?)\/(get\/.+)';
// I can not change anything below.
preg_match("#^$regex#", $url4, $matches);
echo "<pre>";
print_r($matches);
echo "</pre>"
我抓到了 $url4 但抓不到 $url1, $url2, $url3
如果有人提供帮助,我将不胜感激。
使用\K
在最后打印时丢弃先前匹配的字符。
.*(?:\/|^)\K([^\/]+?)\/(get\/.+)
我正在尝试编写 wordpress 漂亮的永久链接正则表达式。
我有以下网址。我需要 2 场比赛,
1st : get/ 之前 / 和 / 之间的最后一个词 第二个:以 get/ 开头的字符串 Url的可能是这样的
http://localhost/akasia/yacht-technical-services/yacht-crew/get/gulets/for/sale/
这里我需要"yacht-crew"和"get/gulets/for/sale/"
http://localhost/akasia/testimonials/get/motoryachts/for/sale/
这里我需要"testimonials"和get/motoryachts/for/sale/
http://localhost/akasia/may/be/lots/of/seperator/but/ineed/last/get/ships/for/rent/
这里我需要"last"和get/ships/for/rent/
$url1 = 'somepage/get/gulets/for/sale'; // I need somepage and get/gulets/for/sale
$url2 = 'somepage/subpage/get/motoryachts/for/rent'; // I need subpage and get/motoryachts/for/rent
$url3 = 'may/be/unlimited/directories/but/i/need/here/get/ships/for/sale'; // I need here and get/ships/for/sale
$url4 = 'services/get/gulets/for/sale'; // I need services and get/gulets/for/sale
$regex = '([^\/]+?)\/(get\/.+)';
// I can not change anything below.
preg_match("#^$regex#", $url4, $matches);
echo "<pre>";
print_r($matches);
echo "</pre>"
我抓到了 $url4 但抓不到 $url1, $url2, $url3
如果有人提供帮助,我将不胜感激。
使用\K
在最后打印时丢弃先前匹配的字符。
.*(?:\/|^)\K([^\/]+?)\/(get\/.+)