-4

pattern contains

  • any words and not and like

    examples - 'adsjfjkdsa not like', 'wordsmsj not like'

  • any words and like

    examples - 'dakladkl like', 'adsjkjk like'

The regex should be single regex not multiple match the follwing patterns:

'jkasjkdsk like'

'anyword not like'

'dsklksd like'

The regex should not match the following words: 'sdkjjksjk notlike' 'sdkjdjsklike' 'jkadjkjkad'

I know that it is possible to check these pattern with two regex patterns. I want to know if it is possible in one regex.

Regex to match first pattern - r'[a-z]*[\s]like'

second pattern - r'[a-z]*[\s]not[\s]like'

bisht
  • 51
  • 8

1 Answers1

1

as simple as this regex : \w+(?: not | )like

you can use :

  • \s instead of (space).
  • [a-zA-Z] instead of \w

note : (?: ) is a non-capturing group. reference : 1) non-capturing group 2) regex doc

Jay Joshi
  • 1,402
  • 1
  • 13
  • 32
  • Thanks.I think \w matches[a-zA-Z0-9_] – bisht Jun 30 '18 at 11:34
  • Yeah, sure it does.! Just a small tip: Don't take the negative votes personally. They are because it was an easy question which you can figure out by looking at some docs about regex. But I feel, No question is small.. And keep looking at the up-votes (+5) you get, as it is far too much important than down(-2 only). Welcome.! – Jay Joshi Jun 30 '18 at 11:41
  • 1
    Yeah. I don't mind votes anyway its just an increment of some variable in someone's code ;) – bisht Jun 30 '18 at 11:55