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 withwww.
.- The
(.*)
captures the entirety of the string.
精彩评论