我有一个MySQL表"products“,其中有一个descriptions字段,其中包含"pack”或"pack of":
Headphones 3 pack, Built-in Microphone - (Latest Model)
Wireless headphones -Bluetooth Headphones 3.5 -3 Pack
Flash Drive-128G-B35 -(12pack)
Wireless headphones -Bluetooth Headphones -(11- pack) Latest Model
Wireless headphones -Bluetooth Headphones -(Pack of 11)
Wireless headphones -Bluetooth Headphones -(packs of 11)我需要一个正则表达式来提取单词"pack“之前和单词"pack of”之后的数字,
产出应是:
3
3
12
11
11
11尝试了很多正则表达式的组合
SELECT @str:="Headphones 3 pack, Built-in Microphone - (Latest Model)" AS str,
regexp_replace(@str, '[^0-9]*(pack)', '')+0 AS packof;这个在单词"pack of“前面的数字仍然不确定它是否涵盖了所有的场景:
SELECT @str:="Wireless headphones -Bluetooth Headphones -(Pack of 11)" AS str,
regexp_replace(@str, '.*pack[^0-9]*', '')+0 AS packof;发布于 2022-01-06 19:22:16
您可以使用
regexp_replace(@str, '(?i)^.*?(?:packs?(?:\\W+of)?\\W*(\\d+)|(\\d+)\\W*pack).*', '$1$2')见regex演示。详细信息
(?i) -不区分大小写的匹配^ -字符串的开始.*? -除断行字符以外的任何零或多个字符,尽可能少。(?:packs?(?:\W+of)?\W*(\d+)|(\d+)\W*pack) --任一种:packs?(?:\W+of)?\W*(\d+) - pack,一个可选的s,一个或多个非字字符的可选序列,然后是of单词,零或多个非字字符,以及组1中捕获的一个或多个数字。| -或(\d+)\W*pack -一个或多个数字捕获到组2,零或多个非字字符,然后是一个pack字符串。.* -除行中断字符以外的任何零或多个字符,尽可能多。$1$2替换将使用组1+组2值替换匹配。
https://stackoverflow.com/questions/70612253
复制相似问题