如何解决无法获取单行oracle中匹配regex_substr的所有匹配项
我正在尝试从oracle 11g的数据库列中获取特定数据,但是我的正则表达式仅使我第一次返回。知道如何在同一行中将所有出现的内容都用“ |”分隔吗???
我的查询:
SELECT regexp_substr(
'"Error:" {user_1@domain.com}<"User_2" {user_2@domain.com};"Error:" {user_3@domain.com}<"User_4" {user_4@domain.com};','Error:[^<]+<'
) AS emails
FROM DUAL;
我的输出应为:
Error:" {user_1@domain.com}< Error:" {user_3@domain.com}<
当前输出为:
Error:" {user_1@domain.com}<
为了清楚地理解,我在表中附加了insert语句的dml:
insert into tests(result) values ('</span></td></tr><tr><td><span class="inputlabel">[14].</span>  <span class="label">ORC|Boston Medical Center|||||||||||||
</span></td></tr><tr><td><span class="inputlabel">[15].</span>  <span class="label">OBR|05-123|LOINC-Lcl-11546-9-1|20050415||||||||||LocalCode: Abscess2||||c|||||
</span></td></tr><tr><td class="errorlabel" nowrap>Error: Report Status Code (ReportStatusCode of type ID) value (c) is invalid Vocabulary code.</td></tr><tr><td class="errorlabel" nowrap>Message rejected.</td></tr><tr><td><span class="inputlabel">[17].</span>  <span class="label">PID||||||||RecCtl_ID|FORTES|AVERY||||||||||||||||||
</span></td></tr><tr><td><span class="inputlabel">[18].</span>  <span class="label">NK1|NK Last Name|NK First Name||||||||||
</span></td></tr><tr><td><span class="inputlabel">[19].</span>  <span class="label">ORC|Boston Medical Center|||||||||||||
</span></td></tr><tr><td><span class="inputlabel">[20].</span>  <span class="label">OBR|05-123|LOINC-Lcl-11546-9-1|20050415||||||||||Local 128477000||||Report_Status_Code 12345678_30|||||
</span></td></tr><tr><td><span class="inputlabel">[21].</span>  <span class="label">OBX|LOINC-Lcl-11546-9-4|SMED-Lcl-78181009-4|||||F|200504231010|BMC
</span></td></tr><tr><td class="inputlabel" nowrap>Processing Results: 3 Messages Accepted,<span class="errorlabel">1 Messages Rejected.</span></td></tr><tr><td class="inputlabel" nowrap>End Time: 2011-08-07 18:47:47.312</td></tr></table>
</span></td></tr><tr><td class="errorlabel" nowrap>Error: Report Status Code (ReportStatusCode of type ID) value (c) is invalid Vocabulary code.</td></tr><tr><td class="errorlabel" nowrap>Message rejected.</td></tr><tr><td><span class="inputlabel">[17].</span>  <span class="label">PID||||||||RecCtl_ID|FORTES|AVERY||||||||||||||||||
')
表格创建:
create table TESTS
(
result CLOB
)
现在,我希望来自以上HTML的所有错误消息,即输出应如下所示:
Error: Report Status Code (ReportStatusCode of type ID) value (c) is invalid Vocabulary code Error: Report Status Code (ReportStatusCode of type ID) value (c) is invalid Vocabulary code
现在我只收到1条错误消息。
解决方法
这回答了问题的原始版本。
如果您需要多个子字符串,那么正确的策略是regexp_replace()
。我认为这是您指定的功能:
SELECT regexp_replace(
'"Error:" {user_1@domain.com}<"User_2" {user_2@domain.com};"Error:" {user_3@domain.com}<"User_4" {user_4@domain.com};','"(Error:[^<]+<)[^;]+;','\1'
) AS emails
FROM DUAL;
Here是db 小提琴。
,我不是特别擅长,但是-看是否有帮助。
- 从第1-4行提取数据
-
TEMP
CTE将字符串分成行 - 最终
select
(从第10行开始)聚合包含“错误”的结果
SQL> select * From v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
CORE 11.2.0.2.0 Production
TNS for 64-bit Windows: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Production
SQL> with test (col) as
2 (select '"Error:" {user_1@domain.com}<"User_2" {user_2@domain.com};"Error:" {user_3@domain.com}<"User_4" {user_4@domain.com};'
3 from dual
4 ),5 temp as
6 (select regexp_substr(col,'[^<;]+',1,level) val
7 from test
8 connect by level <= regexp_count(col,'<') + 1
9 )
10 select listagg(val,'< ') within group (order by val) result
11 from temp
12 where instr(val,'Error') > 0;
RESULT
--------------------------------------------------------------------------------
"Error:" {user_1@domain.com}< "Error:" {user_3@domain.com}
SQL>
使用您发布的示例数据:
SQL> set pagesize 100
SQL> set long 10000
SQL>
SQL> with
2 temp as
3 (select regexp_substr(result,level) val
4 from tests
5 connect by level <= regexp_count(result,'<') + 1
6 )
7 select replace(val,'td class="errorlabel" nowrap>','') result
8 from temp
9 where instr(val,'Error') > 0;
RESULT
--------------------------------------------------------------------------------
Error: Report Status Code (ReportStatusCode of type ID) value (c) is invalid Voc
abulary code.
Error: Report Status Code (ReportStatusCode of type ID) value (c) is invalid Voc
abulary code.
SQL>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。