正则表达式匹配广告而非下载

时间:2013-08-19 03:13:22

标签: regex

我有这一行来阻止广告主机名,例如 ad.somedomain.tld 但它似乎阻止了下载,上传等主机名。

([\w.-]+\.)?ad\.[^/]+/?

什么是正确的语法,谢谢

1 个答案:

答案 0 :(得分:2)

DEMO regex101

<强>正则表达式

\b(?:.?)ad(?:.?)\b

<强> 描述

\b Word boundary: match in between (^\w|\w$|\W\w|\w\W)
(?:.?) Non-capturing Group 
    . 0 to 1 times [greedy] Any character (except newline)
ad Literal ad
(?:.?) Non-capturing Group 
    . 0 to 1 times [greedy] Any character (except newline)
\b Word boundary: match in between (^\w|\w$|\W\w|\w\W)

可视化 (由debuggex提供)

Regular expression visualization