从大字符串oracle regexp中提取字符串

时间:2014-09-11 10:18:44

标签: regex oracle

我有如下字符串。

select b.col1,a.col2,lower(a.col3) from table1 a inner join table2 b on a.col = b.col and a.col = b.col
inner join (select col1, col2, col3,col4 from tablename ) c on a.col1=b.col2    
where
a.col = 'value'

输出需要table1,table2和来自上面字符串的tablename。请告诉我正则表达式以获得结果。

1 个答案:

答案 0 :(得分:0)

应该是一个简单的: - )

SQL> WITH DATA AS(
  2  select q'[select b.col1,a.col2,lower(a.col3) from table1 a inner join table2 b on
  3  a.col = b.col and a.col = b.col inner join (select col1, col2, col3,col4 from tablename )
  4  c on a.col1=b.col2 where a.col = 'value']' str
  5  FROM DUAL)
  6  SELECT LISTAGG(TABLE_NAMES, ' , ') WITHIN GROUP (
  7  ORDER BY val) table_names
  8  FROM
  9    (SELECT 1 val,
 10      regexp_substr(str,'table[[:alnum:]]+',1,level) table_names
 11    FROM DATA
 12      CONNECT BY level <= regexp_count(str,'table')
 13    )
 14  /

TABLE_NAMES
--------------------------------------------------------------------------------
table1 , table2 , tablename

SQL>

简要说明,以便OP /甚至其他人可能会发现它有用:

  • REGEXP_SUBSTR查找单词'table',可以遵循 通过数字或字符串,如1,2,名称等。
  • 要找到所有这些单词,我使用connect by level技术,但它 给出不同行的输出。
  • 最后,将它们作为逗号分隔值放在一行中,I 使用LISTAGG
  • 哦,是的,q'[]'是字符串文字技术。
相关问题