REGEXP_SUBSTR - "substring out of bounds" 错误

REGEXP_SUBSTR - "substring out of bounds" error

我有

select col1,
( REGEXP_SUBSTR ( col2, ' ( ?<=~ ) .*? ( ?=ABCD ) ' ) 
    || SUBSTRING ( col2 FROM POSITION ( 'ABCD' IN col2 ) 
    FOR POSITION ( '~' IN SUBSTRING ( col2 FROM POSITION ( 'ABCD' IN col2 ) ) ) -1 ) as xyz) 
from db.table 
where col2 like '%ABCD%';

我有一个字段,其值如以下模式所述。

Name1#Value1 ~ Name2#Value2 ~ ......... ~ NameX#ValueX ~ ........... ~ NameN#ValueN

名称和值部分的数量没有限制。其中一个名称将具有 'ABCD' 模式。我想提取包含 'ABCD' 模式的名称和值部分并将其放在单独的字段中。

我上面的代码抛出

"substring out of bounds"

错误。

非常感谢您的帮助。谢谢。

当你在寻找一个模式和一个确切的名字时,你不能使用 NVP,但是没有必要混合使用 REGEXP_SUBSUBSTRING

此正则表达式 (~|^)([^~]?ABCD.?#.*?)(~|$) 找到名称中包含 ABCD 的第一个 ~name#value~ 模式:

Trim(Both '~' FROM RegExp_Substr(col2, '(~|^)([^~]*?ABCD.*?)(~|$)',1,1,'i'))

'i' 表示不区分大小写 搜索。

如果您的版本支持(未记录的)RegExp_Substr_gpl,则无需 trim,因为它支持返回特定的捕获组:

RegExp_Substr_gpl( col2, '(~|^)([^~]*?ABCD.*?#.*?)(~|$)',1,1,'i',2)