Creating a Regex expression with a “not” condition for a specific substring

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.

Ex:

  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.

^(.*best)((?!mew).).*$

And have tried

^((?!mew).)(.*best).*$
5

1 Answer

  • Ctrl+F
  • Find what: ^(?=.*best)(?:(?!mew).)*$
  • check Wrap around
  • check Regular expression
  • DO NOT CHECK . matches newline
  • Search in document

Explanation:

^ : 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
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like