3

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).*$
Giacomo1968
  • 53,069
  • 19
  • 162
  • 212
Sri.S
  • 53
  • 1
  • 1
  • 4
  • So you’re looking for lines that contain string1 but not string2? Try to write questions simply and clearly. – Scott - Слава Україні Jul 01 '18 at 02:59
  • 1
    Possible duplicate of [grep for "term" and exclude "another term"](https://superuser.com/questions/537619/grep-for-term-and-exclude-another-term) – Scott - Слава Україні Jul 01 '18 at 02: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 01 '18 at 04: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. – Giacomo1968 Jul 01 '18 at 05: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 01 '18 at 16:00

1 Answers1

8
  • 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
Toto
  • 17,001
  • 56
  • 30
  • 41
  • 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 01 '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 01 '18 at 11:16
  • 1
    @Toto You sir are a genius , it worked brilliantly. Thank you everyone for your time helping me out really appreciate it. – Sri.S Jul 01 '18 at 16:36