0

I am trying to create a "simple" regex to match any string except a specific string in middle. Lets assume that the specific string is apple, Here are results that I expect:

apple -> false
foo-apple -> false
foo-apple-bar -> false
empty-string -> true
foo -> true

I tried:

^(?!apple)$
^(?!apple$)
^.*(?!apple).*$
^(.*(?!apple).*)$

but none of them worked as I expected. This question looks similar but the accepted answer is not working.

Community
  • 1
  • 1
Arashsoft
  • 2,308
  • 4
  • 28
  • 52
  • 1
    http://stackoverflow.com/questions/406230/regular-expression-to-match-line-that-doesnt-contain-a-word. You are looking for `^(?!.*apple)` – Wiktor Stribiżew May 17 '16 at 21:23
  • @WiktorStribiżew, That question has 2k+ votes and it never came in my search results :/. Yes that is what I am looking for. Thanks. – Arashsoft May 17 '16 at 21:30
  • @WiktorStribiżew, your answer surprisingly works while it is different than the accepted answer (accepted answer is `^((?!apple).)*$` ). Is there any question that refer to your answer? – Arashsoft May 17 '16 at 21:39
  • 1
    @WiktorStribiżew, I think I understood your answer. It just avoid (.*apple) string. It is even simpler than the accepted answer. Thanks. – Arashsoft May 17 '16 at 21:50

0 Answers0