Home > Software design >  Regex that doesn't start with character or doesn't contain a substring
Regex that doesn't start with character or doesn't contain a substring

Time:09-16

I would like to create a regex that would match words which doesn't start with $ character or doesn't contain sustrings inherit, initial.

Regex should match:

rgb(0,0,0)
#ffffff
green

and doesn't should match

$green
inherit
initial

CodePudding user response:

You may use this regex with multiple negative lookahead conditions:

^(?!(?:\$|inherit|initial)). 

Which matches start position and then using negative lookahead conditions negates the match using 3 sub matches.

RegEx Demo: https://regex101.com/r/kDpdkC/1

  • Related