You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Nginx正则排除含'toto'子串的域名?

Nginx正则表达式排除指定子串'toto'的正确写法
  • 需求说明:

    • 需要匹配的域名:titi.example.instance.fr、titi-titi.example.instance.fr
    • 需要排除的域名:toto.example.instance.fr、titi-toto.example.instance.fr
  • 你之前的正则问题:^((?!toto).)\.example\.instance\.fr$ 里的 ((?!toto).) 仅能匹配单个字符,无法覆盖titi这类多字符前缀,导致符合要求的域名都匹配失败。

  • 正确正则写法:
    通过负前瞻断言确保整个域名中不存在toto子串,同时匹配完整域名结构:

    ^(?!.*toto).+\.example\.instance\.fr$
    

    拆解说明:

    • ^(?!.*toto):负前瞻规则,从域名开头检查,确保整个字符串里没有toto子串
    • .+:匹配至少一个字符的前缀部分(支持带连字符的格式,比如titi-titi)
    • \.example\.instance\.fr$:精准匹配固定后缀,保证域名结尾符合要求
  • 验证结果:

    • 成功匹配:titi.example.instance.fr、titi-titi.example.instance.fr
    • 自动排除:toto.example.instance.fr、titi-toto.example.instance.fr

内容的提问来源于stack exchange,提问作者khaled lhale

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 19:37:03