I have a use case where I am searching for a particular sub string in a string and if that particular string contains another particular sub string I want it to be rejected.


  1. pikachu_is_the_best_ever_in_the_world_go_pikachu
  2. mew_is_the_best_ever_in_the_world_go_mew
  3. raichu_is_the_best_ever_in_the_world_go_raichu

I want my Regex expression to pick up the string having the word "best" and not the word "mew", i.e the first and third string.

I have tried combining ^(.*best).*$ and ^((?!mew).)*$ into the below expressions and the second regex one only ignores words if "mew" is present in the start of the string.


And have tried

  • So you’re looking for lines that contain string1 but not string2? Try to write questions simply and clearly. – Scott Jul 1 '18 at 2:59
  • 1
    Possible duplicate of grep for "term" and exclude "another term" – Scott Jul 1 '18 at 2:59
  • Sorry this is my first time posting a question. I am trying to write a regex pattern to pick the string that contains the word best and also not contain mew. – Sri.S Jul 1 '18 at 4:30
  • Well, where are you using this Regex? On it’s own? Or in a program? If so what language? Can you set if/else language in that language? The simple solution is to nest one if in another if. – JakeGould Jul 1 '18 at 5:45
  • I cannot use if /else , the application I am working on uses plain regex in its inner logic to find keywords. – Sri.S Jul 1 '18 at 16:00
  • Ctrl+F
  • Find what: ^(?=.*best)(?:(?!mew).)*$
  • check Wrap around
  • check Regular expression
  • DO NOT CHECK . matches newline
  • Search in document


^           : start of line
(?=         : positive lookahead
  .*        : 0 or more any character but newline
  best      : literally "best"
)           : end lookahead
(?:         : start non capture group
  (?!       : negative lookahead, make sure we don't have 
    mew     : literally "mew"
  )         : end lookahead
  .         : any character but newline
)*          : group may appear 0 or more times
$           : end of line
  • I didn't quite understood the last . - why it's needed there? I would expect that it's for matching another char after mew - but it won't work without it, even if mew is the last thing before the end of the line. – arieljannai Jul 1 '18 at 10:43
  • 2
    @arieljannai: Because you need to match that there is no mew at any place within the string, It checks from the beginning of the line that there is not mew followed by 1 character, then iterate (*) all along the string, until the end of string. – Toto Jul 1 '18 at 11:16
  • @Toto You sir are a genius , it worked brilliantly. Thank you everyone for your time helping me out really appreciate it. – Sri.S Jul 1 '18 at 16:36

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.