0

for my pl/sql procedure i have to extract the submatches from a text field and insert those values into another table...

The Regex is something like: /^(xyz|abc)(\w{3,3})(\d{0,2})(\d{2,2})(a|ab|ef)$/

so what i want is basically something like this in pseudo-sql:

select 
  rtbl.1,  -- should return xyz
  rtbl.2,  -- should return GGG
  rtbl.3,  -- should return 1
  rtbl.4,  -- should return xyz
  rtbl.5   -- should return ef
from regex('xyzGGG122ef', /^(xyz|abc)(\w{3,3})(\d{0,2})(\d{2,2})(a|ab|ef)$/) rtbl;

how can this be done?

4

2 回答 2

1

您可以使用regexp_substr获取子匹配:

SELECT 
  regexp_substr('xyzGGG122ef', '^(xyz|abc)(\w{3,3})(\d{0,2})(\d{2,2})(a|ab|ef)$', 1, 1, NULL, 1),
  regexp_substr('xyzGGG122ef', '^(xyz|abc)(\w{3,3})(\d{0,2})(\d{2,2})(a|ab|ef)$', 1, 1, NULL, 2),
  regexp_substr('xyzGGG122ef', '^(xyz|abc)(\w{3,3})(\d{0,2})(\d{2,2})(a|ab|ef)$', 1, 1, NULL, 3),
  regexp_substr('xyzGGG122ef', '^(xyz|abc)(\w{3,3})(\d{0,2})(\d{2,2})(a|ab|ef)$', 1, 1, NULL, 4),
  regexp_substr('xyzGGG122ef', '^(xyz|abc)(\w{3,3})(\d{0,2})(\d{2,2})(a|ab|ef)$', 1, 1, NULL, 5)
FROM dual;

您要获取的组是最后一个参数。在此处阅读更多信息:关于 regexp_substr 的 Oracle 文档

于 2013-10-22T09:22:49.840 回答
0

对上述答案稍作修改:

SELECT
      ROWNUM,
      POS
FROM
      (SELECT
            REGEXP_SUBSTR (
                         'xyzGGG122ef',
                         '^(xyz|abc)(\w{3,3})(\d{0,2})(\d{2,2})(a|ab|ef)$',
                         1,
                         1,
                         NULL,
                         ROWNUM )
                POS
       FROM
            DUAL
       CONNECT BY
            LEVEL <= 5) RTBL;
于 2013-10-22T10:58:28.733 回答