MYSQL Select 查询中的正则表达式
MYSQL REGEX in Select query
需要一个 Regex 解决方案来解决 select 只有列中的字符串模式。
数据如下所示:
Column1
Data Type = String
Data =
"130 - 66||109,118 - 3||102 - 18||109,118 - 2||109,116,149 - 14||141 - 8||130 - 31||102 - 12"
预期结果
66, 3, 18, 2, 14, 8, 31, 12
尝试了正则表达式 - "\-(...*?)\W"
但它不起作用。
MySQL REGEXP
returns 布尔值,不是字符串。
摘自 MySQL 参考手册:
Returns 1
if the string expr
matches the regular expression specified by the pattern pat
, 0
otherwise. If expr
or pat
is NULL
, the return value is NULL
.
也许我们想使用 MySQL REGEXP_REPLACE
功能?
参考:
https://dev.mysql.com/doc/refman/8.0/en/regexp.html#operator_regexp
https://dev.mysql.com/doc/refman/8.0/en/regexp.html#function_regexp-replace
你可以试试这个
SELECT REPLACE(REGEXP_REPLACE('130 - 66||109,118 - 3||102 - 18||109,118 - 2||109,116,149 - 14||141 - 8||130 - 31||102 - 12',
'(([,0-9]+) - )', ''),'||',',');
和结果
66,3,18,2,14,8,31,12
需要一个 Regex 解决方案来解决 select 只有列中的字符串模式。
数据如下所示:
Column1
Data Type = String
Data =
"130 - 66||109,118 - 3||102 - 18||109,118 - 2||109,116,149 - 14||141 - 8||130 - 31||102 - 12"
预期结果
66, 3, 18, 2, 14, 8, 31, 12
尝试了正则表达式 - "\-(...*?)\W"
但它不起作用。
MySQL REGEXP
returns 布尔值,不是字符串。
摘自 MySQL 参考手册:
Returns
1
if the stringexpr
matches the regular expression specified by the patternpat
,0
otherwise. Ifexpr
orpat
isNULL
, the return value isNULL
.
也许我们想使用 MySQL REGEXP_REPLACE
功能?
参考:
https://dev.mysql.com/doc/refman/8.0/en/regexp.html#operator_regexp
https://dev.mysql.com/doc/refman/8.0/en/regexp.html#function_regexp-replace
你可以试试这个
SELECT REPLACE(REGEXP_REPLACE('130 - 66||109,118 - 3||102 - 18||109,118 - 2||109,116,149 - 14||141 - 8||130 - 31||102 - 12',
'(([,0-9]+) - )', ''),'||',',');
和结果
66,3,18,2,14,8,31,12