Analysis of the usage of replace and regexp for regular expression replacement in MySQL

Analysis of the usage of replace and regexp for regular expression replacement in MySQL

This article uses examples to illustrate the use of replace and regexp to replace regular expressions in MySQL. Share with you for your reference, the details are as follows:

Today a friend asked me how to modify the formats similar to “./uploads/110100_cityHotel_北京富豪豪酒店.jpg” found in the database to “./uploads/110100cityHotel北京富豪豪酒店.jpg”. I have never processed data in this way, but I know that MySQL can do it using replace, and regular expressions can also do it.

How to do it?

We only need one sentence like this:

update master_data.md_employee set name=replace(name,"_",'') where id = 825;

-- Note replace(field name,"characters to be replaced","characters to be replaced"), that's it.

In MySQL, replace and regexp are mainly used to replace data through SQL statements.

Let's first talk about the specific usage of replace.

mysql replace usage

1. replace into

Copy the code as follows:
replace into table (id,name) values('1′,'aa'),('2′,'bb')

The purpose of this statement is to insert two records into the table. If the primary key id is 1 or 2 does not exist

is equivalent to

insert into table (id,name) values('1′,'aa'),('2′,'bb')

If the same value exists, the data will not be inserted.

2. replace(object,search,replace)

Replace all occurrences of search in object with replace

select replace('www.jb51.net','w','Ww')

—>WwWwWw.jb51.net

Example: Replace aa in the name field in the table table with bb

update table set name=replace(name,'aa','bb')

Another type of pattern matching provided by MySQL is using extended regular expressions.

When you test for a match on such patterns, use the REGEXP and NOT REGEXP operators (or RLIKE and NOT RLIKE, which are synonyms).

Some characters for extended regular expressions are:

· '.' matches any single character.

· The character class "[...]" matches any character within the square brackets. For example, "[abc]" matches "a", "b", or "c". To name a range of characters, use a "-". "[az]" matches any letter, and "[0-9]" matches any digit.

· “ * ” matches zero or more of the character that precedes it. For example, "x*" matches any number of "x" characters, "[0-9]*" matches any number of digits, and ".*" matches any number of any characters.

If the REGEXP pattern matches anywhere in the value being tested, the pattern matches (this differs from LIKE pattern matching, which matches only if it matches the entire value).

To position a pattern so that it must match the beginning or end of the value being tested, use "^" at the beginning of the pattern or "$" at the end of the pattern.

To illustrate how extended regular expressions work, here's the LIKE query shown above rewritten using REGEXP:

1. To find names that begin with "d", use "^" to match the beginning of the name:

SELECT * FROM master_data.md_employee WHERE name REGEXP '^d';

Such result sets are case-insensitive. If you want to force the REGEXP comparison to be case-sensitive, use the BINARY keyword to make one of the strings a binary string. This query matches only names that start with a lowercase 'd'.

SELECT * FROM master_data.md_employee WHERE name REGEXP BINARY'^d';

To find names ending with "love", use "$" to match the end of the name:

SELECT id,name FROM master_data.md_employee WHERE name REGEXP 'love$';

To find names that contain a "w", use the following query:

SELECT id,name FROM master_data.md_employee WHERE name REGEXP 'w';

Since a regular expression pattern matches if it appears anywhere in a value, you don't have to put a wildcard on either side of the pattern in the previous query to make it match the entire value, as you would if you used an SQL pattern.

To find names that contain exactly 5 characters, use "^" and "$" to match the beginning and end of the name, and 5 instances of "." in between:

SELECT id,name FROM master_data.md_employee WHERE name REGEXP '^.....$';

You can also rewrite the previous query using the "{n}" "repeat n times" operator:

SELECT id,name FROM master_data.md_employee WHERE name REGEXP '^.{5}$';

This is some simple usage of MySQL replace and regexp. For in-depth learning, we will write specific examples and usage in subsequent articles.

PS: Here are two very convenient regular expression tools for your reference:

JavaScript regular expression online testing tool:
http://tools.jb51.net/regex/javascript

Regular expression online generation tool:
http://tools.jb51.net/regex/create_reg

Readers who are interested in more MySQL-related content can check out the following topics: "Summary of MySQL Common Functions", "Summary of MySQL Log Operation Skills", "Summary of MySQL Transaction Operation Skills", "Summary of MySQL Stored Procedure Skills" and "Summary of MySQL Database Lock-Related Skills".

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • Common usage of regular expressions in Mysql
  • Regular Expression (RegExp) determines whether the text box contains special symbols
  • Complete guide to using REGEXP regular expressions in MySQL
  • Detailed explanation of the usage of Oracle regular expression regexp_like
  • js regular expression explanation index attribute (RegExp object)
  • js regular expression RegExp object attributes lastIndex, lastMatch, lastParen, lastContext, rightContext attribute explanation
  • Detailed explanation of regular expression RegExp, a new feature of ES9

<<:  The pitfalls encountered when learning Vue.js

>>:  Detailed explanation of the implementation of WeChat applet track playback and the pitfalls encountered

Recommend

N ways to align the last row of lists in CSS flex layout to the left (summary)

I would like to quote an article by Zhang Xinxu a...

jQuery implements article collapse and expansion functions

This article example shares the specific code of ...

Unbind SSH key pairs from one or more Linux instances

DetachKeyPair Unbind SSH key pairs from one or mo...

How to add automatic completion commands for docker and kubectl on Mac

Introduction to kubectl kubectl is a command line...

Linux uses suid vim.basic file to achieve privilege escalation

Reproduce on Kali First set suid permissions for ...

Summary of the differences between global objects in nodejs and browsers

In Node.js, a .js file is a complete scope (modul...

Pure CSS to modify the browser scrollbar style example

Use CSS to modify the browser scroll bar style ::...

MySQL 5.7.24 installation and configuration graphic tutorial

This article shares the installation and configur...

Introduction to 10 Hooks in React

Table of contents What is ReactHook? React curren...

A brief analysis of adding listener events when value changes in html input

The effect to be achieved In many cases, we will ...

Example code for converting http to https using nginx

I am writing a small program recently. Because th...

Tutorial diagram of installing zabbix2.4 under centos6.5

The fixed IP address of the centos-DVD1 version s...