我有电话号码:
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 Ť Ť
答案 0 :(得分:2)
您似乎想要第一个用大写字母后跟一些数字的字母:
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
答案 1 :(得分:0)
试试这个。寻找两个连续的帽子。我使用NVL()
尝试单次出现大写字母。
为MT0致信
[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
修改强>
如果您需要提取完整的CAPS序列。
你需要这个,
[A-Z]{2,}
答案 2 :(得分:0)
删除&#34;参考&#34;和&#34; Ths&#34;来自电话号码 并且您的代码正常工作
答案 3 :(得分:0)
这是实现目标的一种方式:
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或更多数字后跟空格或行尾的模式。 (如果您想将位数限制为1或更多,请将*
更改为+
。)
我们在&#34;一个或两个大写字母&#34;周围加上括号。将它标记为子表达式,然后我们可以请求在regexp_substr中输出(这是最后一个参数,在我们的例子中是1,因为我们所需的子表达式是遇到的第一个)。
如果您有其他特殊字符可以遵循您想要的模式,那么您只需展开OR部分(目前为( |$)
),例如如果您想要包含问号,OR部分将变为( |?|$)