Salman Arshad
Salman Arshad

Reputation: 272296

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 string.

Upvotes: 3

Views: 72

Answers (1)

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.

Upvotes: 4

Related Questions