简体   繁体   中英

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:

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. In this case, using REGEXP_REPLACE, I would like to remove the substring matched and return 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 supports POSIX regex which doesn't support PERL like properties eg \b , \d etc.

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)
  • [[:<:]] : Match a word boundary (zero width)
  • [0-9]{5} : Match exactly 5 digits
  • [[:blank:]]+ : Match 1 or more of whitespaces (tab or space)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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