Home Database Mysql Tutorial mysql正则表达式字符替换语句

mysql正则表达式字符替换语句

Jun 07, 2016 pm 05:51 PM

本文章来介绍一下在sql语句中使用正则了,这是我们讲述在mysql中使用mysql正则表达式实例,有需要了解的朋友可以参考一下。

 代码如下 复制代码

update comment set url=IF(url REGEXP 'test.yahoo.com.cn',REPLACE(url,'www1.hzhuti.com','www.sina.com'),REPLACE(url,'www2.yahoo.com','www.sina.com')) where 1=1;
update comment set author_url=REPLACE(author_url,'hzhuti','sina') where author_url REGEXP 'www.hzhuti.com';


$ 匹配字符串的结束部分
 $sql ="SELECT 'fonfo' REGEXP '^fo$' from  string_find";
 
 . 匹配任何字符(包括回车和新行)
 $sql ="SELECT 'fofo' REGEXP '^f.*$' from  string_find";
 
 a* 匹配0或多个a字符的任何序列
 
 $sql ="SELECT 'Ban' REGEXP '^Ba*n' from  string_find";
 
 a+ 匹配1个或多个a字符的任何序列
 
 $sql ="SELECT 'Ban' REGEXP '^Ba+n' from  string_find";
 
 
 a? 匹配0个或1个a字符
 
 $sql ="SELECT 'Bn' REGEXP '^Ba?n' from  string_find";
 
 de|abc 匹配序列de或abc
   (abc)* 匹配序列abc的0个或多个实例。
   {1}, {2,3} {n}或{m,n}符号提供了编写的更通用方式,能够匹配模式的很多前述原子(或“部分”)。m和n均为整数。
   o        a* 
 可被写入为a{0,}。
 
 o        a+
 
 可被写入为a{1,}。
 
 o        a?
 
 可被写入为a{0,1}。
 
 更准确地讲,a{n}与a的n个实例准确匹配。a{n,}匹配a的n个或更多实例。a{m,n}匹配a的m~n个实例,包含m和n。
 
 m和n必须位于0~RE_DUP_MAX(默认为255)的范围内,包含0和RE_DUP_MAX。如果同时给定了m和n,m必须小于或等于
  [a-dX], [^a-dX]  匹配任何是(或不是,如果使用^的话)a、b、c、d或X的字符。两个其他字符之间的“-”字符构成一个范围,与从第1个字符开始到第2个字符之间的所有字符匹配。例如,[0-9]匹配任何十进制数字 。要想包含文字字符“]”,它必须紧跟在开括号“[”之后。要想包含文字字符“-”,它必须首先或最后写入。对于[]对内未定义任何特殊含义的任何字符,仅与其本身匹配。

为了说明扩展正则表达式如何工作,上面所示的 LIKE 查询在下面使用 REGEX P重写:
1、为了找出以 “ b ” 开头的名字 , 使用 “ ^ ” 匹配名字的开始并且 “ [bB] ” 匹配小写或大写的 “ b ” :
> SELECT * FROM pet WHERE name REGEXP "^[bB]";

2、为了找出以 “ fy ” 结尾的名字,使用 “ $ ” 匹配名字的结尾:
mysql> SELECT * FROM pet WHERE name REGEXP "fy$";

3、为了找出包含正好 5 个字符的名字,使用 “ ^ ” 和 “ $ ” 匹配名字的开始和结尾 ,和 5 个 “ . ” 实例在两者之间:
mysql> SELECT * FROM pet WHERE name REGEXP "^.....$";

4、你也可以使用 “ {n} ”“ 重复 n 次 ” 操作符重写先前的查询:
mysql> SELECT * FROM pet WHERE name REGEXP "^.{5}$";

 

更多详细内容请查看:

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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 do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

See all articles