【发布时间】: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
我的表列的一个值应为从 0 到 100 的百分比和以下值之一(R、MR、MS 或 S)的串联值。例如:20MS.
我应该使用REGEXP_COUNT 还是SIMILAR TO 来检查数据的有效性。
试过这个但不起作用:
SELECT REGEXP_COUNT('6A', '^[0-9]+(R|MR|MS|S)?')
有什么建议或解决方案吗?
【问题讨论】:
标签: regex amazon-redshift
使用
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])?$'
说明
--------------------------------------------------------------------------------
^ 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
【讨论】:
'^([1-9][0-9]?|100)(M?[RS])?$'