0

Hi I need to find a pattern which randomly occur in a string in any order

The values in a column may be like these

'k=34,n=WER-RTN,l=hy33,z=device'

'k=34,l=hy33,z=device,n=WER-RTN'

'n=WER-RTN,l=hy33,z=device,k=34'

I need to pick up the value against n= till ',' else if it occurs at the end till last. but n can occur any where in the string.

4

1 回答 1

1

如果要获取该WER-RTN值,可以使用如下正则表达式提取该值:

WITH t AS (SELECT 'k=34,n=WER-RTN,l=hy33,z=device' text FROM dual
           UNION
           SELECT 'k=34,l=hy33,z=device,n=WER-RTN' text FROM dual
           UNION
           SELECT 'n=WER-RTN,l=hy33,z=device,k=34' text FROM dual)
SELECT REPLACE(REPLACE(REGEXP_SUBSTR(text,'(n=.+?,)|(n=.+?$)'),'n='),',')
FROM t

如果您使用的是 Oracle 11g 或更高版本,您可以稍微简化一下:

REGEXP_SUBSTR (text, '(^|,)n=([^,]+)', 1, 1, '', 2)
于 2015-11-20T10:24:08.610 回答