1

I want to find matches for string example but not www.example. What is the regex I can use? I've tried the following but it doesn't work:

(?!www.\)example

Ivar
  • 5,377
  • 12
  • 50
  • 56
Shivanand Sharma
  • 404
  • 3
  • 12

1 Answers1

8

If you just try to match a string that does start with example but not with www.example than it would be as easy as:

^example

Otherwise you can use a negative lookbehind:

(?<!\bwww\.)\bexample\b

The \b in here is a word boundery, meaning that it matches it, as long as it is not part of a string. (In other words if it isn't followed by or following any A-Za-z characters, a number or an underscore.)

As mentioned by @CasimiretHippolyte this does still match strings like www.othersubdomain.example.com.

Example

Casimir et Hippolyte
  • 85,718
  • 5
  • 90
  • 121
Ivar
  • 5,377
  • 12
  • 50
  • 56