当前位置:网站首页>MySQL: regexp_ replace
MySQL: regexp_ replace
2022-06-13 07:38:00 【_ seven seven】
Command format :
regexp_replace(source, pattern, replace_string, occurrence)
Parameter description :
● source: string type , The original string to replace .
● pattern: string Type constant , The regular pattern to match ,pattern Throw an exception when the string is empty .
● replace_string:string, Will match pattern String replaced with .
● occurrence: bigint Type constant , Must be greater than or equal to 0,
Greater than 0: Means to replace the number of matches with replace_string,
be equal to 0: Means to replace all matching substrings .
Other types or less than 0 Throw exceptions .
Common cases
1、 use ’#‘ Replace all numbers in the string
SELECT regexp_replace('01234abcde56789','[0-9]','#') AS new_str FROM dual;
result :#####abcde#####
use ’#‘ Replace the number in the string 0、9
SELECT regexp_replace(‘01234abcde56789’,’[09]’,’#’) AS new_str FROM dual;
result :#1234abcde5678#
2、 Skip when encountering non lowercase letters or numbers , From match to 4 Values begin to replace , Replace with ''
SELECT regexp_replace('abcdefg123456ABC','[a-z0-9]','',4)
result :abcefg123456ABC
SELECT regexp_replace('abcDEfg123456ABC','[a-z0-9]','',4)
result :abcDEg123456ABC
SELECT regexp_replace('abcDEfg123456ABC','[a-z0-9]','',7);
result :abcDEfg13456ABC
Skip when encountering non lowercase letters or numbers , Replace all matching values with ''
SELECT regexp_replace('abcDefg123456ABC','[a-z0-9]','',0);
result :DABC
3、 Format the phone number , take +86 13811112222 Convert to (+86) 138-1111-2222,’+‘ There are definitions in regular expressions , Need to escape .\\1 Represents the first group of references
SELECT regexp_replace('+86 13811112222','(\\+[0-9]{2})( )([0-9]{3})([0-9]{4})([0-9]{4})','(\\1)\\3-\\4-\\5',0);
result :(+86)138-1111-2222
SELECT regexp_replace("123.456.7890","([[:digit:]]{3})\\.([[:digit:]]{3})\\.([[:digit:]]{4})","(\\1)\\2-\\3",0) ;
SELECT regexp_replace("123.456.7890","([0-9]{3})\\.([0-9]{3})\\.([0-9]{4})","(\\1)\\2-\\3",0) ;
result :(123)456-7890
4、 Separate characters with spaces ,0 Means to replace all matching substrings .
SELECT regexp_replace('abcdefg123456ABC','(.)','\\1 ',0) AS new_str FROM dual;
result :a b c d e f g 1 2 3 4 5 6 A B C
SELECT regexp_replace('abcdefg123456ABC','(.)','\\1 ',2) AS new_str FROM dual;
result :ab cdefg123456ABC
5、
SELECT regexp_replace("abcd","(.*)(.)$","\\1",0) ;
result :abc
SELECT regexp_replace("abcd","(.*)(.)$","\\2",0) ;
result :d
SELECT regexp_replace("abcd","(.*)(.)$","\\1-\\2",0) ;
result :abc-d
Other cases :
SELECT regexp_replace("abcd","(.)","\\2",1) The result is "abcd", because pattern Only one group is defined in , The second group referenced does not exist .
SELECT regexp_replace("abcd","(.*)(.)$","\\2",0) The result is "d"
SELECT regexp_replace("abcd","(.*)(.)$","\\1",0) The result is "abc"
SELECT regexp_replace("abcd","(.*)(.)$","\\1-\\2",0) The result is "abc-d"
SELECT regexp_replace("abcd","a","\\1",0), The result is ” \1bcd”, Because in pattern There is no group definition in , therefore \1 Direct output to characters .
边栏推荐
- Database outline
- The password does not take effect after redis is set
- 【Emgu.CV】Emgu.CV.Example\OCR运行报错System.IO.FileNotFoundException:“未能加载文件或程序集“System.Drawing.Common
- I always don't understand the high address and high position
- P1434 [SHOI2002] 滑雪 (记忆化搜索
- Redis learning journey -- getting to know redis for the first time
- Time field comparison time size in MySQL
- Pdf to word
- Upgrade the project of log4j to log4j2
- 21 | 面向流水线的指令设计(下):奔腾4是怎么失败的?
猜你喜欢

Redis learning journey - transaction

The password does not take effect after redis is set

Distributed transaction learning (I) preliminary understanding

比较DFS和BFS的优点和缺点及名称词汇

24 | 冒险和预测(三):CPU里的“线程池”

Redis learning journey -- subscription and publishing

IDS persistence ---rdb

MySQL Gtid_ Executed and gtid_ Purged modification time

Redis learning journey master-slave replication

redis-3. Redis list, set, hash, sorted_ set、skiplist
随机推荐
Redis learning journey sentinel mode
Index push down (ICP) for mysql5.6
[Yu Yue education] econometrics reference materials of Jiujiang University
[MySQL change master error] slave is not configured or failed to initialize properly
MySQL summary
Sharp weapon tcpdump
Real time lighting of websocket server based on esp32cam
Redis learning journey -- getting to know redis for the first time
Redis learning journey - cache exceptions (CACHE penetration, cache avalanche, cache breakdown)
在排序数组中查找元素的第一个和最后一个位置
oracle问题,字段里面的数据被逗号隔开,取逗号两边数据
Sorting of numbers and strings
Tidb certification guide PCTA Pctp
[splashsplash] repeat the script that outputs splashsurf
MySQL table cache most detailed notes
Powerdispatcher reverse generation of Oracle data model
平衡二叉树学习笔记------一二熊猫
Distributed transaction learning (I) preliminary understanding
Redis' underlying data structure -- SDS
I always don't understand the high address and high position