开发者

Very simple regular expression question regarding "assertions"

I want a regular expression that matches the following

example.com -> matches, returns example.com
foo-bar.com -> matches, returns foo-bar.com

www.example.com -> does not match
www.foo-bar.com -> does not match

Easy? I just need the PERL compatible regular expression that tests if the string does not begin with www., and if so, captures and returns the whole stri开发者_JAVA百科ng.


You should be able to use this:

 /^(?!www\.)(.*)$/

Explanation:

  • The ^ and $ anchor the match, so it has to match the entire string given.
  • (?!www\.) is a negative lookahead, which blocks the match if the string starts with www..
  • The (.*) captures the entirety of the string.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜