【问题标题】:Regex: Get value between two strings in AWS Redshift正则表达式:获取 AWS Redshift 中两个字符串之间的值
【发布时间】:2021-09-17 14:49:11
【问题描述】:

如何使用正则表达式在 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')
  • @WiktorStribiżew 上帝保佑你:D 这行得通!我会尝试理解并适应它:)

标签: sql regex amazon-redshift


【解决方案1】:

你可以使用

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

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

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

REGEXP_SUBSTR docs 中的附加参数是:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    • 2011-08-04
    相关资源
    最近更新 更多