Oracle listagg regexp_substr用于代码提取和连接

时间:2014-10-15 08:55:03

标签: sql regex oracle oracle11g regexp-substr

我在Oracle 11g数据库上使用PL / SQL v10 我的代码存储在我需要提取的问题表的描述列中。 要做到这一点,我正在制作正则表达式which works fine in 101regex但在oracle中失败, 我会假设我使用的语法不正确。

select '''' || listagg(regexp_substr(q.questiondescription,'(LIF|LPA) ?\d{1,2}.\d{1,2}(\.\d{1})?'), ''', ''') 
within group (order by q.questionid) || '''' 
from question q
where q.isthunderheadonly = 0 or q.isthunderheadonly is null

我需要匹配的模式:

LIF 1.2 Both
LIF 2.7.1 Address Line 1
LIF 4.13 Occupation
LIF 10.6.1 Address Line 1
LPA0.1 What type of LPA do you want?
LPA0.2 Do you have same attorneys with your partner ?

我的正则表达式出了什么问题?

编辑:我得到的结果

'LIF 3.1', 'LIF 4.1', 'LIF 4.2', 'LIF 5.1', 'LIF 7.1', 'LPA0.1', 'LPA0.2'
在我想到的第二组之后,它忽略了一切。

2 个答案:

答案 0 :(得分:2)

(LIF |LPA)\d{1,2}(.\d{1,2})*(\.\d{1})?

OR

(LIF |LPA)[([:digit:]|.)]*

这会匹配。

演示:

SQL> l
  1  with my_data(str,num) as
  2  (
  3  select 'LIF 1.1.1 First Name',1 from dual
  4  union all
  5  select 'LIF 1.2 Date Of Birth' ,1 from dual
  6  union all
  7  select 'LIF 1.2 Date Of Birth' ,2 from dual
  8  union all
  9  select 'LIF 7.10 How many other properties do you own?',1 from dual
 10  union all
 11  select 'DT 05. Do you have children?',1 from dual
 12  union all
 13  select 'LIF 15 Notes to solicitor',1  from dual
 14  union all
 15  SELECT 'LPA0.2 Do you have same attorneys with your partner' ,1 from dual
 16  )
 17  select str, regexp_substr(str,'(LIF |LPA)\d{1,2}(.\d{1,2})*(\.\d{1})*') regex1,
 18  regexp_substr(str,'(LIF |LPA)[([:digit:]|.)]*') regex2
 19  from my_data
 20* group by str
SQL> /

STR                                                          REGEX1               REGEX2
------------------------------------------------------------ ---------------------------
LPA0.2 Do you have same attorneys with your partner          LPA0.2               LPA0.2
DT 05. Do you have children?
LIF 1.1.1 First Name                                         LIF 1.1.1            LIF 1.1.1
LIF 1.2 Date Of Birth                                        LIF 1.2              LIF 1.2
LIF 15 Notes to solicitor                                    LIF 15               LIF 15
LIF 7.10 How many other properties do you own?               LIF 7.10             LIF 7.10

6 rows selected.

答案 1 :(得分:1)

我改变正则表达式,希望它能在你的oracle上正常工作:)

L(IF|PA\d{1,}(\.\d{1,}){0,1}) {1,}(\d{1,}(\.\d{1,}){0,}){0,1}

Regular expression visualization

Debuggex Demo