C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
MySQL NOT REGEXP OperatorNOT 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. Otherwise, the result is 1. SyntaxThe following is a basic syntax to use this operator in MySQL: expression NOT REGEXP pattern ; In this syntax, the expression is an input string on which we will perform searching for matching the regular expression. And pattern represents the regular expression for which we are testing the string. This syntax is generally used with the SELECT statements. The above syntax is equivalent to the below statement: NOT (expression REGEXP pattern) ; Let us understand how this operator works in MySQL through examples. ExampleThe below statement is the most basic example to use the NOT REGEXP operator. Here we have just used a string and compare it with a regular expression to check whether any part of the input string matches the pattern or not. mysql> SELECT 'Corner' NOT REGEXP 'Corn' AS Result1, 'Bread' NOT REGEXP 'Bro' AS Result2; Here is the result: The below statement is another example where the pattern matches if the given string starts with Java. Since the given string matched with Java but due to the negation of the REGEXP operator, we will get the output 0. mysql> SELECT NOT ('TheDeveloperBlog' REGEXP '^Java') AS 'Result'; Here is the result: Suppose we have a table named employee that contains the following data. If we want to get the employee detail whose name does not start with j or s, we can do this as follows: mysql> SELECT * FROM employee WHERE Name NOT REGEXP '^[js]'; Executing the statement, we will get the desired result. See the below output:
Next TopicMySQL regexp Operator
|