Oracle 11g get all matched occurrences by a regular expression(Oracle 11g 通过正则表达式获取所有匹配的事件)
问题描述
我正在使用 Oracle 11g,我想使用 REGEXP_SUBSTR 来匹配给定模式的所有出现.例如
I'm using Oracle 11g and I would like to use the REGEXP_SUBSTR to match all the occurrences for a given pattern. For example
SELECT
REGEXP_SUBSTR('Txa233141b Ta233141 Ta233142 Ta233147 Ta233148',
'(^|s)[A-Za-z]{2}[0-9]{5,}(s|$)') "REGEXP_SUBSTR"
FROM DUAL;
仅返回第一个匹配项 Ta233141,但我想返回与正则表达式匹配的其他匹配项,即 Ta233142 Ta233147 Ta233148.
returns only the first match Ta233141 but I would like to return the other occurrences that match the regex, meaning Ta233142 Ta233147 Ta233148.
推荐答案
REGEXP_SUBSTR 只返回一个值.您可以将字符串转换为伪表,然后查询匹配项.目前有一种基于 XML 的方法可以做到这一点,但只要您只有一个源字符串,就可以使用 connect-by:
REGEXP_SUBSTR only returns one value. You could turn your string into a pseudo-table and then query that for matches. There's an XML-based way of doing this that escapes me at the moment, but using connect-by works, as long as you only have one source string:
SELECT REGEXP_SUBSTR(str, '[^ ]+', 1, LEVEL) AS substr
FROM (
SELECT 'Txa233141b Ta233141 Ta233142 Ta233147 Ta233148' AS str FROM DUAL
)
CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(str, '[^ ]+')) + 1;
...给你:
SUBSTR
--------------------
Txa233141b
Ta233141
Ta233142
Ta233147
Ta233148
...并且您可以使用原始模式的稍微简单的版本对其进行过滤:
... and you can filter that with a slightly simpler version of your original pattern:
SELECT substr
FROM (
SELECT REGEXP_SUBSTR(str, '[^ ]+', 1, LEVEL) AS substr
FROM (
SELECT 'Txa233141b Ta233141 Ta233142 Ta233147 Ta233148' AS str
FROM DUAL
)
CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(str, '[^ ]+')) + 1
)
WHERE REGEXP_LIKE(substr, '^[A-Za-z]{2}[0-9]{5,}$');
SUBSTR
--------------------
Ta233141
Ta233142
Ta233147
Ta233148
这不是很漂亮,但也不是在一个字段中保存多个值.
Which isn't very pretty, but neither is holding multiple values in one field.
这篇关于Oracle 11g 通过正则表达式获取所有匹配的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Oracle 11g 通过正则表达式获取所有匹配的事件
基础教程推荐
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01