Home >Database >Mysql Tutorial >How to use the regexp_replace function in mysql

How to use the regexp_replace function in mysql

王林
王林forward
2023-06-02 11:16:052106browse

1. The function replaces the substring matching the pattern pat in the string expr with repl and returns the replacement result.

2. If expr, pat or repl is NULL, the function returns NULL.

The REGEXP_REPLACE() function supports the following optional parameters:

pos means starting the search from the specified position of the string expr. By default, matching starts from the first character.

occurence indicates how many matching substrings to replace. The default value is 0, which means replace all matching substrings.

match_type indicates the matching method, and the parameters are consistent with the above REGEXP_LIKE() function.

Example

mysql> SELECT REGEXP_REPLACE('a b c', 'b', 'X');
+-----------------------------------+
| REGEXP_REPLACE('a b c', 'b', 'X') |
+-----------------------------------+
| a X c                             |
+-----------------------------------+
mysql> SELECT REGEXP_REPLACE('abc def ghi', '[a-z]+', 'X', 1, 3);
+----------------------------------------------------+
| REGEXP_REPLACE('abc def ghi', '[a-z]+', 'X', 1, 3) |
+----------------------------------------------------+
| abc def X                                          |
+----------------------------------------------------+

The above is the detailed content of How to use the regexp_replace function in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete