Oracle REGEXP_SUBSTR | Fetch string between two delimiters

懵懂的女人 提交于 2021-02-05 11:42:34

问题


I have a string Organization, INC..Truck/Equipment Failure |C. I want to fetch the sub-string after organization name (after two '..' characters) and before pipe character. So the output string should be - Truck/Equipment Failure. Can you please help.

I have been trying forming regexp like this but doesn't seem working.

select regexp_substr('Organization, INC..Truck/Equipment Failure |C', '[^.]+',1,2) from dual;


回答1:


You may use this.

SELECT REGEXP_SUBSTR ('Organization, INC..Truck/Equipment Failure |C',
                      '([^.]+)\|',
                      1,
                      1,
                      NULL,
                      1)
  FROM DUAL;

EDIT: This will match exactly two dots followed by one or more characters other than a | till the end of string.

SELECT REGEXP_SUBSTR ('Organization, INC..Truck/Equipment Failure',
                      '\.{2}([^|]+)',
                      1,
                      1,
                      NULL,
                      1)
  FROM DUAL;

DEMO




回答2:


Classic SUBSTR + INSTR option:

SQL> with test as (select 'Organization, INC..Truck/Equipment Failure |C' col from dual)
  2  select substr(col, instr(col, '..') + 2,
  3                     instr(col, '|') - instr(col, '..') - 2
  4                ) result
  5  from test;

RESULT
------------------------
Truck/Equipment Failure

SQL>


来源:https://stackoverflow.com/questions/49027346/oracle-regexp-substr-fetch-string-between-two-delimiters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!