dsgs8208
2012-03-20 17:37 阅读 17
已采纳

正则表达式:排除匹配

I have this pattern:

~^([a-z0-9]+[a-z0-9-]+[a-z0-9]+\.([a-z]+)(\.[a-z]+)?)$~i

It will match the following:

xxx.xxx or xxx.xxx.xxx

(number of x per doesn't matter)

How do I disallow xxx.xxx.xxx if the set of characters before the first . is wwww ?

For example, it should allow example.co or example.co.uk but it should not allow www.example.co or www.example.co.uk

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dongshun1884 dongshun1884 2012-03-20 17:44

    You can use a negative lookahead/lookbehind, eg:

    ~^((?!www\.)[a-z0-9]+[a-z0-9-]+[a-z0-9]+\.([a-z]+)(\.[a-z]+)?)$~i
    
    点赞 评论 复制链接分享

相关推荐