如何使用 regexp_substr

how to use regexp_substr

我有电话号码:

TX353 G35 1992
Ref QP141 B151 R4 1956
RM216 M285 K5 1996
T385 C22 1960
Ths LB500 M200 A5 1998

我想要结果:

TX353 G35 1992          =>TX
Ref QP141 B151 R4 1956  =>QP
RM216 M285 K5 1996      =>RM
T385 C22 1960           =>T
Ths LB500 M200 A5 1998  =>LB

我使用:

SELECT REGEXP_SUBSTR(callnumber,'[A-Z]+')

结果不正确 TX R R M 吨 T

试试这个。寻找两个连续的上限。我使用 NVL() 尝试获取单个出现的大写字母。

Credits to MT0 for that

[A-Z]{2}

示例:

with my_data(str) as
(
  select 'TX353 G35 1992' from dual
  union all
  select 'Ref QP141 B151 R4 1956' from dual
  union all
  select 'RM216 M285 K5 1996' from dual
  union all
  select 'T385 C22 1960' from dual
  union all
  select 'Ths LB500 M200 A5 1998' from dual

)
  select str,NVL(regexp_substr(str,'[A-Z]{2,}'),regexp_substr(str,'([A-Z]+)\d',1,1,NULL,1)) from my_data;

输出:

TX353 G35 1992          TX
Ref QP141 B151 R4 1956  QP
RM216 M285 K5 1996      RM
T385 C22 1960           T
Ths LB500 M200 A5 1998  LB

编辑:

If you need to extract the the full sequence of CAPS.

You need this, [A-Z]{2,}

从电话号码中删除 "Ref" 和 "Ths" 并且您的代码有效

您似乎想要第一个大写字母后跟一些数字:

Oracle 设置:

CREATE TABLE your_table ( your_column ) AS
  SELECT 'TX353 G35 1992'         FROM DUAL UNION ALL
  SELECT 'Ref QP141 B151 R4 1956' FROM DUAL UNION ALL
  SELECT 'RM216 M285 K5 1996'     FROM DUAL UNION ALL
  SELECT 'T385 C22 1960'          FROM DUAL UNION ALL
  SELECT 'Ths LB500 M200 A5 1998' FROM DUAL UNION ALL
  SELECT 'Ref A123 B456 C7 2000'  FROM DUAL;

查询:

SELECT REGEXP_SUBSTR(
         your_column,
         '([A-Z]+)\d',
         1,     -- Start at the first character
         1,     -- Get the first match
         NULL,  -- Case sensitive
         1      -- Return the first capture group
       ) As match
FROM   your_table

输出:

MATCH
-----
TX
QP
RM
T
LB
A

这是一种方法:

WITH sample_data AS (select 'TX353 G35 1992' str from dual union all
                     select 'Ref QP141 B151 R4 1956' str from dual union all
                     select 'RM216 M285 K5 1996' str from dual union all
                     select 'T385 C22 1960' str from dual union all
                     select 'Ths LB500 M200 A5 1998' str from dual union all
                     select 'X12345' str from dual union all
                     select 'Y F123' str from dual)
SELECT str,
       regexp_substr(str, '([A-Z]{1,2})[[:digit:]]*( |$)', 1, 1, NULL, 1) sub_str
FROM   sample_data;

STR                    SUB_STR
---------------------- ----------------------
TX353 G35 1992         TX
Ref QP141 B151 R4 1956 QP
RM216 M285 K5 1996     RM
T385 C22 1960          T
Ths LB500 M200 A5 1998 LB
X12345                 X
Y F123                 Y

这将查找一个或两个大写字母后跟 0 个或多个数字后跟 space 或行尾的模式。 (如果要将位数限制为 1 或更多,请将 * 更改为 +。)

我们将 "one or two upper case letters" 括起来,将其标记为子表达式,然后我们可以请求在 regexp_substr 中输出它(这是最后一个参数,在我们的例子中是 1 因为我们想要的子表达式是遇到的第一个)。

如果您有其他特殊字符可以遵循您想要的模式,那么您可以简单地扩展 OR 部分(目前 ( |$)),例如如果您想包含问号,则 OR 部分将变为 ( |?|$)