【发布时间】:2017-02-08 17:43:35
【问题描述】:
使用 PHP,我想检查 (true/false) robots.txt 文件中是否有连续的“用户代理”指令。
使用这个正则表达式,preg_match('~User-agent:\h*(?:\R|$)~i', $string) 我找到了所有 'User-agent:' 行,但我还没有找到如何检测连续行。
User-agent: # 'User-agent:'
\h* # horizontal whitespace (0 or more times)
(?: # group, but do not capture:
\R # '\R' (any Unicode newline sequence)
| # OR
$ # before an optional \n, and the end of the string
) # end of grouping
例如
User-agent: 008
user-agent: Accoona
User-Agent: Googlebot
User-Agent: aipbot*
disallow: /
结果:正确
User-Agent: Googlebot
Crawl-delay: 60
User-agent: aipbot*
disallow: /
结果:错误
User-agent: 008
Crawl-delay: 2
user-agent: Accoona
User-Agent: Googlebot
User-Agent: aipbot*
disallow: /
结果:正确
【问题讨论】:
标签: php regex robots.txt