Home Database Mysql Tutorial MYSQL入门学习之二:使用正则表达式搜索_MySQL

MYSQL入门学习之二:使用正则表达式搜索_MySQL

Jun 01, 2016 pm 01:38 PM
expression

正则表达式

bitsCN.com


MYSQL入门学习之二:使用正则表达式搜索

 

一、正则表达式介绍

正则表达式是用来匹配文本的特殊的串(字符集合)。

 

二、使用MySQL正则表达式

 

1、MySQL仅仅支持多数正则表达式实现的一个很小的子集。

 

2、LIKE匹配整个列值;而REGEXP匹配列值的子串,如果被匹配的文本在列值中出现,REGEXP将会找到它,相应的行将被返回。看下面的例子:

[sql] 

mysql>select username from v9_admin where username like 'space';  

Emptyset (0.00 sec)  

   

mysql>select username from v9_admin where username REGEXP 'space';  

+-----------+  

|username  |  

+-----------+  

|warmspace |  

+-----------+  

1 rowin set (0.02 sec)  

3、默认MySQL中的正则表达式匹配不区分大小写,可使用BINARY关键字区分大小写,如

 

[sql] 

mysql>select username from v9_admin  

    -> where username REGEXP BINARY'SPACCE';  

Emptyset (0.04 sec)  

4、进行OR匹配:为搜索两个串之一,使用|,如下所示:

 

[sql] 

mysql>select roleid from v9_admin_role  

    -> where roleid REGEXP '1|3';  

+--------+  

|roleid |  

+--------+  

|      1 |  

|      3 |  

+--------+  

2 rowsin set (0.00 sec)  

5、匹配几个字符之一:匹配任何单一字符。如下所示:

 

[sql] 

mysql>select name from v9_collection_node  

    -> where name REGEXP '005-[123]';  

+-----------------+  

|name            |  

+-----------------+  

|101-02-01-005-2 |  

|101-02-01-005-1 |  

|101-02-01-005-3 |  

+-----------------+  

3 rowsin set (0.00 sec)  

正如所见,[]是另一种形式的OR语句。'005-[123]'与’005-[1|2|3]’是等价的。

 

匹配除给定字符外的字符:

 

[sql] 

mysql>select name from v9_collection_node  

    -> where name REGEXP '005-[^123]';  

+-------------------+  

|name              |  

+-------------------+  

|101-02-01-005-4.2 |  

|101-02-01-005-4   |  

|101-02-01-005-5   |  

+-------------------+  

3 rowsin set (0.00 sec)  

6、可使用-来定义一个范围。如[1-9],[a-b]。

 

[sql] 

mysql>select name from v9_collection_node  

    -> where name REGEXP '005-[1-3]';  

+-----------------+  

|name            |  

+-----------------+  

|101-02-01-005-2 |  

|101-02-01-005-1 |  

|101-02-01-005-3 |  

+-----------------+  

3 rowsin set (0.00 sec)  

7、为了匹配特殊字符,必须用//为前导,例如//-,//.等。这种处理即转义(escaping)。

 

多数正则表达式实现使用单个反斜杠转义特殊字符,但MYSQL要求两个(MYSQL自己解释一个,正则表达式库解释另一个)。

 

//也用来引用元字符(具有特殊含义的字符),如下表:

 

MYSQL入门学习之二:使用正则表达式搜索_MySQL

 

8、为了更方便工作,可以使用预定义的字符集,称为字符类(character class)。

 

[sql] 

mysql> select name fromv9_collection_node  

   -> where name REGEXP '[[:digit:]]';  

+-------------------+  

| name              |  

+-------------------+  

| 101-10-01-002-1   |  

| 101-02-01-005-4.2 |  

| 101-02-01-005-2   |  

| 101-02-01-005-1   |  

| 101-02-01-005-3   |  

| 101-02-01-005-4   |  

| 101-02-01-005-5   |  

| 101-10-01-002-2   |  

| 101-11-04-001-1   |  

+-------------------+  

9 rowsin set (0.00 sec)  


MYSQL入门学习之二:使用正则表达式搜索_MySQL
 

9、使用正则表达式重复元字符匹配多个实例:

 

[sql] 

mysql> select name fromv9_collection_node  

   -> where name REGEXP '1{2}';  

+-----------------+  

| name            |  

+-----------------+  

| 101-11-04-001-1 |  

+-----------------+  

1 rowin set (0.00 sec)  


MYSQL入门学习之二:使用正则表达式搜索_MySQL
 

10、定位符(为了匹配特定位置的文本):

 

[sql] 

mysql> select name from v9_collection_node  

    -> where name REGEXP '2$';  

+-------------------+  

| name              |  

+-------------------+  

| 101-02-01-005-4.2 |  

| 101-02-01-005-2   |  

| 101-10-01-002-2   |  

+-------------------+  

3 rows in set (0.00 sec)  


MYSQL入门学习之二:使用正则表达式搜索_MySQL
 

11、使REGEXP起类似LIKE的作用,它们的区别在于LIKE匹配整个串而REGEXP匹配子串。利用定位符,通过用^开始每个表达式,用$结束每个表达式。

 

12、简单的正则表达式测试:可以在不使用数据库表的情况下用SELECT来测试正则表达式。REGEXP检查总是返回0或1(匹配)。

 

[sql] 

mysql> select 'hello' REGEXP'[0-9]';  

+------------------------+  

| 'hello' REGEXP '[0-9]' |  

+------------------------+  

|                      0 |  

+------------------------+  

1 row in set (0.00 sec)  

 

bitsCN.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve Python expression syntax errors? How to solve Python expression syntax errors? Jun 24, 2023 pm 05:04 PM

Python, as a high-level programming language, is easy to learn and use. Once you need to write a Python program, you will inevitably encounter syntax errors, and expression syntax errors are a common one. In this article, we will discuss how to resolve expression syntax errors in Python. Expression syntax errors are one of the most common errors in Python, and they are usually caused by incorrect usage of syntax or missing necessary components. In Python, expressions usually consist of numbers, strings, variables, and operators. most common

In C and C++, comma is used to separate expressions or statements In C and C++, comma is used to separate expressions or statements Sep 09, 2023 pm 05:33 PM

In C or C++, the comma "," has different uses. Here we will learn how to use them. Commas as operators. The comma operator is a binary operator that evaluates the first operand, discards the result, then evaluates the second operand and returns the value. The comma operator has the lowest precedence in C or C++. Example #include<stdio.h>intmain(){ intx=(50,60); inty=(func1(),func2());} Here 60 will be assigned to x. For the next statement, func1( will be executed first

Introduction to exponential function expressions in C language Introduction to exponential function expressions in C language Feb 18, 2024 pm 01:11 PM

Introduction to how to write exponential function expressions in C language and code examples What is an exponential function? The exponential function is a common type of function in mathematics. It can be expressed in the form of f(x)=a^x, where a is the base and x is the exponent. . Exponential functions are mainly used to describe exponential growth or exponential decay. Code example of exponential function In C language, we can use the pow() function in the math library to calculate the exponential function. The following is a sample program: #include

lambda expression in Java lambda expression in Java Jun 09, 2023 am 10:17 AM

Lambda expressions in Java With the release of Java 8, lambda expressions have become one of the most concerned and discussed topics among Java developers. Lambda expressions can simplify Java programmers' tedious writing methods, and can also improve the readability and maintainability of programs. In this article, we will take a deep dive into lambda expressions in Java and how they provide a simpler and more intuitive programming experience in Java code.

Loop over a collection using lambda expressions Loop over a collection using lambda expressions Feb 19, 2024 pm 07:32 PM

A lambda expression is an anonymous function that can be conveniently used to iterate over a collection. In this article, we will introduce how to use lambda expressions to iterate over collections, and provide specific code examples. In Python, the syntax format of a lambda expression is as follows: lambda parameter list: The parameter list of an expression lambda expression can contain one or more parameters, separated by commas. The expression is the return value of the lambda function. Let's look at a simple example below, assuming

Java Lambda Expression in Practice: Unlocking the Mysteries of Functional Programming with Code Java Lambda Expression in Practice: Unlocking the Mysteries of Functional Programming with Code Feb 26, 2024 am 10:25 AM

Lambda expression, as the name suggests, is an anonymous function with the arrow symbol (->) as its core. It allows you to pass blocks of code as arguments to other methods, or store them into variables for later use. Lambda expression syntax is concise and easy to understand, and it is very suitable for processing data flow and parallel computing. 1. The basic syntax of Lambda expression The basic syntax of Lambda expression is as follows: (parameter list)->{code block} Among them, the parameter list and code block are optional. If there is only one parameter, the parentheses can be omitted. If the code block is only one line, the curly braces can be omitted. For example, the following code block uses a Lambda expression to add 1 to a number: List

Advanced Guide to Python Lambda Expressions: From Beginner to Mastery Advanced Guide to Python Lambda Expressions: From Beginner to Mastery Feb 24, 2024 pm 03:31 PM

Introduction and basic syntax of Lambda expressions Lambda expressions consist of a function parameter list, a colon and a function body. The function parameter list is the same as that of an ordinary function, and the function body is an expression rather than a set of statements. #Example: Return a function that receives two numbers and returns their sum sum=lambdax,y:x+y Application scenarios of Lambda expressions Lambda expressions are very suitable for use as callback functions, filter functions and mapping functions. Callback function: A callback function is a function called within another function. Lambda expressions make it easy to create callback functions without declaring their names. Filter function: The filter function is used to filter out full

The try statement block in PHP8.0 supports expressions The try statement block in PHP8.0 supports expressions May 14, 2023 am 08:12 AM

With the rapid development of computer technology, programming languages ​​are constantly being upgraded and improved. Among them, PHP, as a commonly used web development language, is constantly innovating and launching new versions. Recently, the release of PHP8.0 version has attracted widespread attention. Among them, the improvements to the exception handling mechanism in the new version have attracted a lot of attention. This article will focus on the topic of expression support in the try statement block in PHP8.0. 1. Improvements in the exception handling mechanism of PHP8.0 In previous versions, P

See all articles