如何使用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
相关产品推荐
相关产品推荐

