首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Regexp或substr或另一种查找字符串的方法

Regexp或substr或另一种查找字符串的方法
EN

Stack Overflow用户
提问于 2019-04-09 08:47:43
回答 3查看 84关注 0票数 2

我想达到最好的性能,只在单词DL:之后选择一个"string“。

我有一个包含值的列(varchar2):

代码语言:javascript
复制
    DL:1011909825
    Obj:020190004387 DL:8010406429
    Obj:020190004388 DL:8010406428
    DL:190682
    DL:PDL01900940
    Obj:020190004322 DL:611913067

因此,产出应该是:

代码语言:javascript
复制
    1011909825
    8010406429
    8010406428
    190682
    PDL01900940
    611913067

我不擅长正则表达式,但我尝试了regexp_replace:

代码语言:javascript
复制
regexp_replace(column,'Obj:|DL:','',1, 0, 'i')

这几乎可以,但输出仍然不一样:

代码语言:javascript
复制
    1011909825
    020190004387 8010406429
    020190004388 8010406428
    190682
    PDL01900940
    020190004322 611913067

如何解决这个问题&达到最佳性能

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-04-09 08:50:24

如果数据总是这样,那么SUBSTR + INSTR将完成以下工作:

代码语言:javascript
复制
SQL> with test (col) as
  2    (
  3      select 'DL:1011909825' from dual union all
  4      select 'Obj:020190004387 DL:8010406429' from dual union all
  5      select 'Obj:020190004388 DL:8010406428' from dual union all
  6      select 'DL:190682' from dual union all
  7      select 'DL:PDL01900940' from dual union all
  8      select 'Obj:020190004322 DL:611913067' from dual
  9     )
 10  select col, substr(col, instr(col, 'DL:') + 3) result
 11  from test;

COL                            RESULT
------------------------------ ------------------------------
DL:1011909825                  1011909825
Obj:020190004387 DL:8010406429 8010406429
Obj:020190004388 DL:8010406428 8010406428
DL:190682                      190682
DL:PDL01900940                 PDL01900940
Obj:020190004322 DL:611913067  611913067

6 rows selected.

SQL>

REGEXP_SUBSTR可能如下所示:

代码语言:javascript
复制
 <snip>
 10  select col,
 11         ltrim(regexp_substr(col, 'DL:\w+'), 'DL:') resul
 12  from test;

COL                            RESULT
------------------------------ -----------------------------
DL:1011909825                  1011909825
Obj:020190004387 DL:8010406429 8010406429
Obj:020190004388 DL:8010406428 8010406428
DL:190682                      190682
DL:PDL01900940                 PDL01900940
Obj:020190004322 DL:611913067  611913067

如果有大量的数据,这应该比正则表达式快得多。

票数 1
EN

Stack Overflow用户

发布于 2019-04-09 09:04:00

你可以从中得到一些想法。

代码语言:javascript
复制
DL:(.*)

Match 1
1.  1011909825
Match 2
1.  8010406429
Match 3
1.  8010406428
Match 4
1.  190682
Match 5
1.  PDL01900940
Match 6
1.  611913067

https://rubular.com/r/jKjcPs8sPr4Ifn

票数 2
EN

Stack Overflow用户

发布于 2019-04-09 08:56:33

substr + instr将具有更好的性能,但是如果您想使用regexp:

代码语言:javascript
复制
-- substr + instr will have better performance
with s (str) as (
select 'DL:1011909825' from dual union all
select 'Obj:020190004387 DL:8010406429' from dual union all
select 'Obj:020190004388 DL:8010406428' from dual union all
select 'DL:190682' from dual union all
select 'DL:PDL01900940' from dual union all
select 'Obj:020190004322 DL:611913067' from dual)
select str, regexp_substr(str, 'DL:(.*)', 1, 1, null, 1) rs
from s;

STR                            RS                            
------------------------------ ------------------------------
DL:1011909825                  1011909825                    
Obj:020190004387 DL:8010406429 8010406429                    
Obj:020190004388 DL:8010406428 8010406428                    
DL:190682                      190682                        
DL:PDL01900940                 PDL01900940                   
Obj:020190004322 DL:611913067  611913067                     

6 rows selected.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55588624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档