Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

MySQL NOT REGXP operator

NOT REGXP operator

MySQL NOT REGXP is used to perform a pattern match of a string expression expr against a pattern pat. The pattern can be an extended regular expression.

Syntax:

expr NOT REGEXP pat

Argument

Name Description
expr string expression
pat A pattern which is not to be matched.

The function returns 1 if expr matches pat; otherwise, it returns 0. If either expr or pat is NULL, the result is NULL

MySQL Version: 5.6

Video Presentation

Example -1: MySQL NOT REGXP operator

The above MySQL statement will find the name of the country not beginning with ‘U’. The ‘^’ have been used to match the beginning of the name.

Code:

SELECT * FROM author 
WHERE country NOT REGEXP '^U';

Sample table: author


Sample Output:

mysql> SELECT * FROM author 
    -> WHERE country NOT REGEXP '^U';
+--------+----------------------+-----------+----------------+
| aut_id | aut_name             | country   | home_city      |
+--------+----------------------+-----------+----------------+
| AUT002 | William Maugham      | Canada    | Toronto        | 
| AUT004 | S.B.Swaminathan      | India     | Bangalore      | 
| AUT005 | Thomas Morgan        | Germany   | Arnsberg       | 
| AUT009 | Marquis de Ellis     | Brazil    | Rio De Janerio | 
| AUT011 | John Betjeman Hunter | Australia | Sydney         | 
| AUT012 | Evan Hayek           | Canada    | Vancouver      | 
| AUT013 | E. Howard            | Australia | Adelaide       | 
+--------+----------------------+-----------+----------------+
7 rows in set (0.17 sec)

PHP script

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example-not-regexp-function - php mysql examples | w3resource</title>
<meta name="description" content="example-not-regexp-function - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>A list of Authors with their id, name country and home city. The list set against a condition, and that is, Author's name should not begin with 'U':</h2>
<table class='table table-bordered'>
<tr>
<th>Author's id</th><th>Author's name</th><th>Country</th><th>home_city</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT *
FROM author
WHERE country NOT REGEXP "^U"') as $row) {
echo "<tr>";
echo "<td>" . $row['aut_id'] . "</td>";
echo "<td>" . $row['aut_name'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['home_city'] . "</td>";
echo "</tr>";  
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

JSP script

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example-not-regexp-function</title>
</head>
<body>
<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String Host = "jdbc:mysql://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data ="SELECT * FROM author WHERE country NOT REGEXP '^U'";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Author's ID</td>
<td>Author's name</td>
<td>Country</td>
<td>Home City</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("aut_id")%></TD>
<TD><%=rs.getString("aut_name")%></TD>
<TD><%=rs.getString("country")%></TD>
<TD><%=rs.getString("home_city")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Cant connect to database.");
}
%>
</body>
</html>

Example -2: MySQL NOT REGXP operator

The following MySQL statement will find the author’s name not ending with ‘on’ and not ending with ‘an’. The ‘$’ character have been used to match the ending of the name.

SELECT * FROM author 
WHERE aut_name NOT REGEXP "on$" 
AND aut_name NOT REGEXP "an$";

Sample table: author


Sample Output:

 mysql> SELECT * FROM author 
    -> WHERE aut_name NOT REGEXP "on$" 
    -> AND aut_name NOT REGEXP "an$";
+--------+----------------------+-----------+----------------+
| aut_id | aut_name             | country   | home_city      |
+--------+----------------------+-----------+----------------+
| AUT002 | William Maugham      | Canada    | Toronto        | 
| AUT003 | William Anthony      | UK        | Leeds          | 
| AUT008 | Nikolai Dewey        | USA       | Atlanta        | 
| AUT009 | Marquis de Ellis     | Brazil    | Rio De Janerio | 
| AUT011 | John Betjeman Hunter | Australia | Sydney         | 
| AUT012 | Evan Hayek           | Canada    | Vancouver      | 
| AUT013 | E. Howard            | Australia | Adelaide       | 
| AUT014 | C. J. Wilde          | UK        | London         | 
| AUT015 | Butler Andre         | USA       | Florida        | 
+--------+----------------------+-----------+----------------+
9 rows in set (0.01 sec)
 

PHP script

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example1-not-regexp-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-not-regexp-function - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>A list of Authors with their id, name country and home city. The list set against a condition, and that is, Author's name should not end with 'on' and also not with 'an':</h2>
<table class='table table-bordered'>
<tr>
<th>Author's id</th><th>Author's name</th><th>Country</th><th>Home city</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT *
FROM author 
WHERE aut_name NOT REGEXP "on$"
AND aut_name NOT REGEXP "an$"') as $row) {
echo "<tr>";
echo "<td>" . $row['aut_id'] . "</td>";
echo "<td>" . $row['aut_name'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['home_city'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Online Practice Editor:


All String Functions

MySQL String Functions, slide presentation

Previous: NOT LIKE
Next: OCTET_LENGTH