首页 > 解决方案 > 如何在oracle sql查询中提取括号之间的文本

问题描述

我正在尝试从字符串中提取括号之间的值。

例如,我有这个字符串:

No information was found [AI1234]. 

我想得到括号之间的结果,即AI1234.

然而,表达方式并不总是相同的。它可能会有所不同。

我正在尝试编写这样的查询:

REGEXP_SUBSTR(mssg, '\((.+)\)', 1, 1, NULL, 1) AS "description" from book

但它没有返回任何东西。我错过了什么?

我也已经尝试过类似的方法,问题是支架长度可能会有所不同。所以下面这个会返回一些东西,但不是我要找的东西:

substr(mssg,instr(mssg,'(')-8,10) as "description"

标签: sqloracle

解决方案


如果您正在寻找方括号之间的一组数字,请尝试以下操作:

WITH
indata(msg) AS (
  SELECT 'No information was found [1234]'
)
SELECT
  REGEXP_SUBSTR(
     msg                    -- the string
   , '^[^[]+[[](\d+)[]].*$' -- the pattern (with a captured
                            -- string "\d+" in round parentheses)
   , 1                      -- start from position 1
   , 1                      -- first found occurrence
   , ''                     -- no modifiers
   , 1                      -- first captured group
   ) AS extr
FROM indata;
 extr 
------
 1234

推荐阅读