我正在尝试使用 Oracle 的REGEXP_SUBSTR()
函数从文本字段中提取分钟数。
数据:
Treatment of PC7, PT1 on left. 15 min.
15 minutes.
15 minutes
15 mins.
15 mins
15 min.
15 min
15min
15
在每种情况下,我都希望提取字符串的“15”部分。
尝试:
\d+
获取所有数值,包括“7”和“1”,这是不可取的。(\d)+(?=\ ?min)
从除最后一行之外的所有行中获取“15”。(?((\d)+(?=\ ?min))((\d)+(?=\ ?min))|\d+)
,一个条件语句,不匹配任何东西。
我的条件语句有什么问题?
** 编辑 **
WITH DATA AS (
SELECT 'Treatment of PC7, PT1 on left. 15 min.' COMMENTS FROM DUAL
UNION ALL
SELECT '15 minutes.' COMMENTS FROM DUAL
UNION ALL
SELECT '15 minutes' COMMENTS FROM DUAL
UNION ALL
SELECT '15 mins.' COMMENTS FROM DUAL
UNION ALL
SELECT '15 mins' COMMENTS FROM DUAL
UNION ALL
SELECT '15 min.' COMMENTS FROM DUAL
UNION ALL
SELECT '15 min' COMMENTS FROM DUAL
UNION ALL
SELECT '15min' COMMENTS FROM DUAL
UNION ALL
SELECT '15' COMMENTS FROM DUAL
)
SELECT COMMENTS,
REGEXP_SUBSTR(COMMENTS, '(\d+)\s?(?:min.*)?$', 1, 1) A,
REGEXP_SUBSTR(COMMENTS, '\d+?(?= ?min)|^\d+$', 1, 1) B,
REGEXP_SUBSTR(COMMENTS, '\d+?(?: ?min)|^\d+$', 1, 1) C
FROM DATA
结果(必须有比“代码示例”更好的方式来格式化列):
COMMENTS A B C
Treatment of PC7, PT1 on left. 15 min.
15 minutes.
15 minutes
15 mins.
15 mins
15 min.
15 min
15min
15 15 15 15