是否有可能使正则表达式中的字符无效

时间:2015-01-16 10:17:11

标签: regex wordpress mod-rewrite permalinks

我正在尝试写wordpress相当永久链接正则表达式。

我有以下网址。我需要2场比赛,

1st:/ /之前的最后一个词/ 2nd:以get /开头的字符串 Url可能就像这些

http://localhost/akasia/yacht-technical-services/yacht-crew/get/gulets/for/sale/

在这里,我需要“游艇船员”和“get / gulets / for / sale /”

http://localhost/akasia/testimonials/get/motoryachts/for/sale/

这里我需要“推荐”并获得/ motoryachts / for / sale /

http://localhost/akasia/may/be/lots/of/seperator/but/ineed/last/get/ships/for/rent/

这里我需要“最后”并获得/ ship / 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

如果有人帮忙,我感激不尽。

1 个答案:

答案 0 :(得分:0)

使用\K丢弃之前匹配的字符,以便在最后一次打印。

.*(?:\/|^)\K([^\/]+?)\/(get\/.+)

DEMO