Tutorial on using regular expressions to search in mysql (1)
This topic will learn how to use regular expressions in the MySQL where clause to control data filtering.
Introduction to regular expressions
The filtering examples in the first two chapters allow matching, comparison, and wildcard operators to find data. For basic filtering (or even some not-so-basic filtering), this is enough. But as the complexity of the filter conditions increases, the complexity of the where clause itself also increases.
This is where regular expressions become useful. Regular expressions are special strings (sets of characters) used to match text. If you want to extract phone numbers from a text file, you can use regular expressions. If you need to find all files with a number in the middle of their name, you can use a regular expression. If you want to find all repeated words in a text section, you can use regular expressions. If you want all URLs in a page to be the actual html links of those URLs, you can also use regular expressions.
All programming languages, text editors, operating systems, etc. support regular expressions. Programmers and network administrators have been paying attention to regular expressions as an important part of their technical tools for a long time.
Regular expressions are created using the regular expression language, a special language used to do all the work just discussed and more. Like any language, regular expressions have your Special syntax and instructions must be learned.
Since regular expressions are so useful, what is the relationship between regular expressions and MySQL? As mentioned before, the function of regular expressions is to match text, comparing a pattern (regular expression) with a text string. MySQL provides preliminary support for regular expressions using the where clause, allowing you to specify regular expressions to filter the data retrieved by select.
Using MySQL regular expressions
Basic character matching
We start with a very simple example. The following statement retrieves all rows where column prod_name contains the text 1000:
select prod_name from products where prod_name regexp '1000' order by prod_name;
except key This statement looks very much like a statement using like, except that the word like is replaced by regexp. It tells MySQL that what follows regexp is treated as a regular expression (a regular expression that matches the text body 1000).
Why bother using regular expressions? In the previous example, regular expressions really didn't bring much benefit, but consider the following example:
select prod_name from products where prod_name regexp '.000'order by prod_name;
The regular expression .000 is used here. . is a special character in the regular expression language. It means matching any character. Therefore, 1000 and 2000 both match and return
. Of course, this special example can also be completed using like and wildcard characters.
There is an important difference between like and regexp. Please look at the following two statements:
select prod_name from products where prod_name like '1000' order by prod_name;
select prod_name from products where prod_name regexp '1000' order by prod_name;
If you execute the above two statements, you will find that the first statement does not return data, but the second statement returns a row. Why?
like matches the entire column. If the matched text appears in a column value, like will not find it and the corresponding row will not be returned (unless wildcards are used). And regexp matches within the column value. If the matched text appears in the column value, regexp will find it and the corresponding row will be returned. This is a very important difference.
So, can regexp be used to match the entire column value and play the same role as like? The answer is fixed, just use the ^ and $ locators.
The above is the detailed content of Tutorial on using regular expressions to search in mysql (1). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use Baidu Advanced Search Baidu search engine is currently one of the most commonly used search engines in China. It provides a wealth of search functions, one of which is advanced search. Advanced search can help users search for the information they need more accurately and improve search efficiency. So, how to use Baidu advanced search? The first step is to open the Baidu search engine homepage. First, we need to open Baidu’s official website, which is www.baidu.com. This is the entrance to Baidu search. In the second step, click the Advanced Search button. On the right side of the Baidu search box, there is

How does Xianyu search for users? In the software Xianyu, we can directly find the users we want to communicate with in the software. But I don’t know how to search for users. Just view it among the users after searching. Next is the introduction that the editor brings to users about how to search for users. If you are interested, come and take a look! How to search for users in Xianyu? Answer: View details among the searched users. Introduction: 1. Enter the software and click on the search box. 2. Enter the user name and click Search. 3. Select [User] under the search box to find the corresponding user.

In the era dominated by intelligence, office software has also become popular, and Wps forms are adopted by the majority of office workers due to their flexibility. At work, we are required not only to learn simple form making and text entry, but also to master more operational skills in order to complete the tasks in actual work. Reports with data and using forms are more convenient, clear and accurate. The lesson we bring to you today is: The WPS table cannot find the data you are searching for. Why please check the search option location? 1. First select the Excel table and double-click to open it. Then in this interface, select all cells. 2. Then in this interface, click the "Edit" option in "File" in the top toolbar. 3. Secondly, in this interface, click "

PHP regular expression verification: Number format detection When writing PHP programs, it is often necessary to verify the data entered by the user. One of the common verifications is to check whether the data conforms to the specified number format. In PHP, you can use regular expressions to achieve this kind of validation. This article will introduce how to use PHP regular expressions to verify number formats and provide specific code examples. First, let’s look at common number format validation requirements: Integers: only contain numbers 0-9, can start with a plus or minus sign, and do not contain decimal points. floating point

The mobile Taobao app software provides a lot of good products. You can buy them anytime and anywhere, and everything is genuine. The price tag of each product is clear. There are no complicated operations at all, making you enjoy more convenient shopping. . You can search and purchase freely as you like. The product sections of different categories are all open. Add your personal delivery address and contact number to facilitate the courier company to contact you, and check the latest logistics trends in real time. Then some new users are using it for the first time. If you don’t know how to search for products, of course you only need to enter keywords in the search bar to find all the product results. You can’t stop shopping freely. Now the editor will provide detailed online methods for mobile Taobao users to search for store names. 1. First open the Taobao app on your mobile phone,

To validate email addresses in Golang using regular expressions, follow these steps: Use regexp.MustCompile to create a regular expression pattern that matches valid email address formats. Use the MatchString function to check whether a string matches a pattern. This pattern covers most valid email address formats, including: Local usernames can contain letters, numbers, and special characters: !.#$%&'*+/=?^_{|}~-`Domain names must contain at least One letter, followed by letters, numbers, or hyphens. The top-level domain (TLD) cannot be longer than 63 characters.

In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.

Alibaba Cloud Disk, this popular storage tool, not only helps us manage personal resources efficiently, but also provides many convenient functions. So many users may not be able to find cloud disk resources when searching, so they want to search all resources in the entire disk. So below, the editor of this site will answer this question in detail and share the specific search method. Users who want to know, please come and follow this article to learn more! How to search other people's resources in Alibaba Cloud Disk 1. First, search the specific path of the resource file in the folder directory of Alibaba Cloud Disk to find the corresponding folder. 2. Then use the file search function and enter the keywords you want to find to find the relevant file content. 3. Then we share the link with others to directly locate and download
