问题描述
我正在尝试编写wordpress漂亮的永久链接正则表达式.
I'm trying to write wordpress pretty permalinks regex.
我有以下网址.我需要2场比赛,
I have following urls. I need 2 matches,
1st:/和/之间的最后一个字,在get/之前2nd:以get/开头的字符串网址可能是这样的
1st : last word between / and / before get/2nd : string which is start with get/Url's may be like these
http://localhost/akasia/yacht -technical-services/游艇船员/get/gulets/for/sale/
在这里,我需要游艇船员"和获取/gulets/出售/出售/"
Here I need "yacht-crew" and "get/gulets/for/sale/"
http://localhost/akasia/testimonials/get/motoryachts/for/sale /
在这里我需要推荐"并获得/摩托车/用于/出售/
here I need "testimonials" and get/motoryachts/for/sale/
http://localhost/akasia/may/be/lots/of/seperator/but/ineed/last/get/ships/for/rent/
在这里我需要最后一个"并获得/发货/获得/租金/
here I need "last" and 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
I catch $url4 but Could not $url1, $url2, $url3
如果有人帮忙,我会感激不尽.
I appreciate if someone help.
推荐答案
使用\K
丢弃先前匹配的字符,使其从最终打印中消失.
Use \K
to discard the previously matched characters from printing at the final.
.*(?:\/|^)\K([^\/]+?)\/(get\/.+)
这篇关于是否有可能使正则表达式中的字符无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!