Home > front end >  Regex: combines not equals, not contained in and match up until
Regex: combines not equals, not contained in and match up until

Time:01-19

I need a regex that:

  • won't match if the string is equal to any of a series of strings
  • won't match if any of another series of sub-strings is contained anywhere within
  • and will match only up until any of a set of certain delimiter characters.

I've got the first two requirements, but can't figure out how to add in the last part:

^(?:((?!^fill$)(?!^style$)(?!->)[^;:]))*$

Will not match 'fill' or 'style' (but will match 'fills' or 'astyle'). Will not match if '->' is anywhere inside. (e.g. don't match 'a->b') However a : or a ; will cause no match, rather than matching up until the first occurrence of either of those characters. e.g.

Will:

should return 'Will' but currently returns nothing.

CodePudding user response:

You can use

^(?!(?:fill|style)$|.*(?:-[>-]|<-))[^;:] 

See the regex demo.

Details:

  • ^ - start of string
  • (?!(?:fill|style)$|.*(?:-[>-]|<-)) - immediately to the right, there can't be:
    • (?:fill|style)$ - fill or style (followed by the end of string)
    • | - or
    • .*(?:-[>-]|<-) - after any zero or more chars as many as possible, --, ->, <- (note the <-> alternative is missing since <- covers it)
  • [^;:] - any zero or more chars other than ; and : as many as possible
  • Related