【问题标题】:Oracle listagg regexp_substr for code extraction and concatinationOracle listagg regexp_substr 用于代码提取和连接
【发布时间】:2014-12-10 06:48:10
【问题描述】:

我正在 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'

我认为第二组之后它会忽略所有内容。

【问题讨论】:

  • 你匹配什么,你不匹配什么? (LIF|LPA) 后面有一个空格,LPA0.1 What type of LPA do you want? 中不存在该空格。
  • They were wrong@Avinash.
  • @vks 没飞,抱歉
  • @AvinashRaj 没有这样做
  • 如果你想捕获所有然后在捕获组中包围模式regex101.com/r/dZ1vT6/15使用\s*而不是`?`

标签: sql regex oracle oracle11g regexp-substr


【解决方案1】:
(LIF |LPA)\d{1,2}(.\d{1,2})*(\.\d{1})?

(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.

【讨论】:

  • 它产生与其他 'LIF 3.1', 'LIF 4.1', 'LIF 4.2', 'LIF 5.1', 'LIF 7.1', 'LPA0.1', 'LPA0.2' 相同的结果,尽管我必须承认将空间放入 (LIF |LPA) 非常棒!
【解决方案2】:

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

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

Debuggex Demo

【讨论】:

  • 不抱歉,我需要捕获LIF 1.2LIF 2.7.1,...LPA0.2
  • 编辑后会生成'LIF 3.1', 'LIF 4.1', 'LIF 4.2', 'LIF 5.1', 'LIF 7.1', 'LPA0.1 ', 'LPA0.2 ',因此会跳过LIF 2.7.1LIF 10.6.1
  • @LIUFA 是的,所以我再更新一次,看看你想要什么
  • 相同的结果,它缺少 LIF 2.7.1LIF 10.6.1
  • 它确实匹配,但我最初的示例也是如此,这意味着 oracle 的正则表达式语法与 python 不同。谢谢你帮助我顺便说一句。
猜你喜欢
  • 2019-05-22
  • 2020-10-10
  • 2021-04-23
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
相关资源
最近更新 更多