简体   繁体   English

REGEX_REPLACE 不匹配从开头到第一次出现的 5 位数单词的所有字符

[英]REGEX_REPLACE not matching all chars from the beginning to the first occurrence of a 5 digits word

I've this record in a Mysql table:我在 Mysql 表中有这条记录:

ADDRESS
----------------------------------
sdasd 4354 ciao 12345 sdsdsa asfds 

I would like to match all chars from the beginning to the first occurrence of a 5 digits word, including it.我想匹配从开头到第一次出现的 5 位数单词的所有字符,包括它。 In this case, using REGEXP_REPLACE, I would like to remove the substring matched and return sdsdsa asfds .在这种情况下,使用 REGEXP_REPLACE,我想删除匹配的 substring 并返回sdsdsa asfds

What I've tried to do is this:我试图做的是:

SELECT REGEXP_REPLACE(ADDRESS, '^.+\b\d{5}\b.','') FROM `mytable`

The regexp seems to work testing it in this snippet and I cannot understand why Mysql won't.正则表达式似乎可以在这个片段中对其进行测试,我不明白为什么 Mysql 不会。

MySQL supports POSIX regex which doesn't support PERL like properties eg \b , \d etc. MySQL 支持不支持 PERL 等属性的 POSIX 正则表达式,例如\b\d等。

This regex should work for you:这个正则表达式应该适合你:

SELECT REGEXP_REPLACE
('sdasd 4354 ciao 12345 sdsdsa asfds', '^.+[[:<:]][0-9]{5}[[:blank:]]+', '') as val;

+--------------+
| val          |
+--------------+
| sdsdsa asfds |
+--------------+

RegEx Details:正则表达式详细信息:

  • ^.+ : Match 1 or more of any characters at the start (greedy) ^.+ :在开头匹配 1 个或多个任意字符(贪婪)
  • [[:<:]] : Match a word boundary (zero width) [[:<:]] :匹配单词边界(零宽度)
  • [0-9]{5} : Match exactly 5 digits [0-9]{5} : 准确匹配 5 位数字
  • [[:blank:]]+ : Match 1 or more of whitespaces (tab or space) [[:blank:]]+ :匹配 1 个或多个空格(制表符或空格)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM