正则表达式:获取 AWS Redshift 中两个字符串之间的值

Regex: Get value between two strings in AWS Redshift

如何使用正则表达式在 redshift 中提取两个子字符串之间的特定值?

我在 redshift 的一列中有如下所示的字符串:

[{'code': 'bla', 'amount': '149.30', 'type': 'fixed_amount'}]

我想提取浮点数。 我使用了几个正则表达式,但似乎 redshift 不接受它们。

select order_id, discount_codes, regexp_substring(discount_codes, '''amount'': ''[^'']*') as value from orders_shopify_de

给我这个错误:

ERROR: function regexp_substring(character varying, "unknown") does not exist Hint

又如:

regexp_replace(discount_codes, '(?<=''amount'': '')(.*)(?='',)')

给我这个错误:

ERROR: function regexp_substring(character varying, "unknown") does not exist Hint

有没有办法提取浮点数?

提前致谢!

您可以使用

REGEXP_SUBSTR(discount_codes, '''amount'': ''([^'']*)', 1, 1, 'e')

模式是'amount': '([^']*)匹配

  • 'amount': ' - 'amount': ' 字符串
  • ([^']*) - 第 1 组:除 ' 个字符之外的任何零个或多个字符。

REGEXP_SUBSTR docs 中的附加参数是:

  • 1 - 从字符串中的第一个字符开始搜索
  • 1 - 告诉正则表达式引擎提取第一次出现的模式
  • 'e' - 启用从结果匹配中提取子表达式(又名 捕获组 值)。