正则表达式重定向陷入无限循环

时间:2017-10-02 01:06:25

标签: regex wordpress

所以,我试图获取任何以select ath_id, avg(record) from (select ath_id, record from atheletes as t1 where (select count(*) from atheletes where t1.ath_id=ath_id and record > t1.record) < 3) as d group by ath_id; 开头并以4级深度结束的网址,以便将/ 1追加到最后,例如

'locations'应该去 http://example.com/locations/lorem/ipsum/dolor

我有以下我认为是正确的,但似乎给了我一个无限的重定向循环:

FROM:http://example.com/locations/lorem/ipsum/dolor/1

TO:locations/(.+)/(.+)/(.+)

知道我哪里出错了吗?

2 个答案:

答案 0 :(得分:0)

(。+)将匹配任何char直到... / dolor /。这就是为什么你可能会得到无限循环。

您可以考虑使用/ [a-z] /来解析网址中的每个子集

答案 1 :(得分:0)

问题在于您没有充分限制模式。

http://example.com/locations/lorem/ipsum/dolor/1
                   ||||||||||           |     |
                   locations/(.+       )/(.+ )/(.+)

http://example.com/locations/lorem/ipsum/dolor/1/1
                   ||||||||||                 | |__
                   ||||||||||                 |    |
                   locations/(.+             )/(.+)/(.+)

http://example.com/locations/lorem/ipsum/dolor/1/1/1
                   ||||||||||                   | |__
                   ||||||||||                   |    |
                   locations/(.+               )/(.+)/(.+)

所以替换

locations/(.+)/(.+)/(.+)

使用下列其中一项取决于使用的正则表达式方法:

locations/([^/]*)/([^/]*)/([^/]*)\z    # Perl, PCRE, ...

locations/([^/]*)/([^/]*)/([^/]*)$     # JavaScript, ...