Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex not equal to string

Tags:

.net

regex

I'm banging my head against a wall with a regular expression. I'm trying to define an expression that excludes exactly this text 'System' (case insensitive), but can contain the word 'System' providing it's not just that.

Examples:

  • System == INVALID
  • SYSTEM == INVALID
  • system == INVALID
  • syStEm == INVALID
  • asd SysTem == Valid
  • asd System asd == Valid
  • System asd == Valid
  • asd System == Valid
  • asd == Valid
like image 448
Kieron Avatar asked Jun 03 '10 09:06

Kieron


People also ask

What does '$' mean in regex?

Literal Characters and Sequences For instance, you might need to search for a dollar sign ("$") as part of a price list, or in a computer program as part of a variable name. Since the dollar sign is a metacharacter which means "end of line" in regex, you must escape it with a backslash to use it literally.

What is not in regular expression?

NOT REGEXP in MySQL is a negation of the REGEXP operator used for pattern matching. It compares the given pattern in the input string and returns the result, which does not match the patterns. If this operator finds a match, the result is 0.

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.


1 Answers

Try this:

^(?!system$) 

Or this to match the whole line:

^(?!system$).*$ 

The regex has a negative look-ahead on its beginning, which doesn't match if "system" is the entire string.

like image 85
Kobi Avatar answered Oct 01 '22 17:10

Kobi