从给定字符串中提取子字符串

时间:2011-10-10 12:36:52

标签: oracle plsql oracle10g

我的数据如下

1)MAXO_INSTR_INTERFACE    
2)MAXIS_VENDOR_INTERFACE
3)MAXIMOS_EMPS_INTERFACE2

我需要提取位于PL / SQL中两个下划线之间的String

INPUT                    EXPECTED OUTPUT
------------------------ ---------------
MAXO_INSTR_INTERFACE     INSTR   
MAXIS_VENDOR_INTERFACE   VENDOR  
MAXIMOS_EMPS_INTERFACE2  EMPS

我尝试过使用子字符串函数,但我无法准确执行。

3 个答案:

答案 0 :(得分:5)

稍微简单的正则表达式:

SQL> with t as
  2  ( select 'maxo_instr_interface' as txt from dual union all
  3    select 'maxis_vendor_interface' from dual union all
  4    select 'maximos_emps_interface2' from dual
  5  )
  6  select txt
  7       , regexp_substr(txt,'[^_]+',1,2)
  8    from t
  9  /

TXT                     REGEXP_SUBSTR(TXT,'[^_]
----------------------- -----------------------
maxo_instr_interface    instr
maxis_vendor_interface  vendor
maximos_emps_interface2 emps

3 rows selected.

的问候,
罗布。

答案 1 :(得分:3)

使用SUBSTR

with strings as (
  select 'MAXO_INSTR_INTERFACE' as string from dual
  union all
  select 'MAXIS_VENDOR_INTERFACE' from dual
  union all
  select 'MAXIMOS_EMPS_INTERFACE2' from dual
)
select substr(string,
              instr(string, '_', 1, 1) + 1,
              instr(string, '_', 1, 2) - instr(string, '_', 1, 1) - 1
              ) as substr from strings;

返回:

SUBSTR
---------------------------------------------------------------------
INSTR
VENDOR
EMPS

但正则表达式解决方案更容易理解。

问题还有一个PL / SQL标签:

create or replace function f (p_str in varchar2) return varchar2 as
  v_begin constant pls_integer := instr(p_str, '_', 1, 1) + 1;
  v_len constant pls_integer := instr(p_str, '_', 1, 2) - v_begin;
begin
  return substr(p_str, v_begin, v_len);
end;

返回:

begin
  dbms_output.put_line(f('MAXO_INSTR_INTERFACE'));
  dbms_output.put_line(f('MAXIS_VENDOR_INTERFACE'));
  dbms_output.put_line(f('MAXIMOS_EMPS_INTERFACE2'));
end;
/

INSTR
VENDOR
EMPS

PL/SQL procedure successfully completed.

答案 2 :(得分:1)

这将在下划线之间返回字符串:

WITH t AS (SELECT 'MAXO_INSTR_INTERFACE' AS txt FROM DUAL
           UNION
           SELECT 'MAXIS_VENDOR_INTERFACE' AS txt FROM DUAL
           UNION
           SELECT 'MAXIMOS_EMPS_INTERFACE2' AS txt FROM DUAL) 
SELECT REGEXP_REPLACE( txt, '(^.*\_)([[:alnum:]]*)(\_.*$)', '\2' )
  FROM t;

返回:

INSTR  
VENDOR
EMPS 

正则表达式REGEXP_REPLACE( txt, '(^.*\_)([[:alnum:]]*)(\_.*$)', '\2' )查找第一个下划线,然后查找下一个下划线的任何字母数字,最后输入的其余部分,然后用它找到的第二个部分替换所有内容(即下划线之间的部分)。

如果下划线之间的文本中有空格,那么使用REGEXP_REPLACE( txt, '(^.*\_)(([[:alnum:]]|[[:space:]])*)(\_.*$)', '\2' ),对于REGEXP匹配可能性的完整下拉,这里有一篇好文章:

http://orafaq.com/node/2404

希望它有所帮助...