【问题标题】:Pattern match check in RedshiftRedshift 中的模式匹配检查
【发布时间】:2020-11-30 20:42:21
【问题描述】:

我的表列的一个值应为从 0 到 100 的百分比和以下值之一(R、MR、MS 或 S)的串联值。例如:20MS.

我应该使用REGEXP_COUNT 还是SIMILAR TO 来检查数据的有效性。 试过这个但不起作用:

SELECT REGEXP_COUNT('6A', '^[0-9]+(R|MR|MS|S)?')

有什么建议或解决方案吗?

【问题讨论】:

    标签: regex amazon-redshift


    【解决方案1】:

    使用

    where column SIMILAR TO '[0-9]+(R|MR|MS|S)'
    

    如果百分比后面的值是可选的,使用

    where column SIMILAR TO '[0-9]+(R|MR|MS|S)?'
    

    使用正则表达式运算符~可以实现

    where column ~ '^[0-9]+(M?[RS])?$'
    

    regex proof

    说明

    --------------------------------------------------------------------------------
      ^                        the beginning of the string
    --------------------------------------------------------------------------------
      [0-9]+                   any character of: '0' to '9' (1 or more
                               times (matching the most amount possible))
    --------------------------------------------------------------------------------
      (                        group and capture to \1 (optional
                               (matching the most amount possible)):
    --------------------------------------------------------------------------------
        M?                       'M' (optional (matching the most amount
                                 possible))
    --------------------------------------------------------------------------------
        [RS]                     any character of: 'R', 'S'
    --------------------------------------------------------------------------------
      )?                       end of \1 (NOTE: because you are using a
                               quantifier on this capture, only the LAST
                               repetition of the captured pattern will be
                               stored in \1)
    --------------------------------------------------------------------------------
      $                        before an optional \n, and the end of the
                               string
    

    【讨论】:

    • 感谢您的解决方案。这在数字部分接受任何数字。我们如何验证数字部分在 0 到 100 之间。
    • @SatishTakkalapalli 你可以take this one'^([1-9][0-9]?|100)(M?[RS])?$'
    猜你喜欢
    • 2015-03-24
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    相关资源
    最近更新 更多