Oracle 11g 通过正则表达式获取所有匹配的事件

2021-12-05 00:00:00 regex oracle

我正在使用 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.

相关文章