如何使用REGEXP_SUBSTR返回所有结果?

时间:2014-12-23 12:18:22

标签: regex oracle

我需要返回四个结果,只显示三个。我怎么能这样做?

查询:

SELECT REGEXP_SUBSTR(lines, '[0-9]{1,3}', 1, 1,'m')
from 
(
      SELECT '111 - first line' lines FROM dual
      UNION
      SELECT '222 - second line'      FROM dual
      UNION
      SELECT '333 - third  line
              444 - fourth line' FROM dual
)

它返回了这个:

111
222
333

我想要这个:

111
222
333
444

这可能吗?

1 个答案:

答案 0 :(得分:1)

首先让我们制作一些数据:

CREATE TABLE some_table (some_data VARCHAR2(200))
/
INSERT INTO some_table VALUES ('111 - first line')
/
INSERT INTO some_table VALUES ('222 - second line')
/
INSERT INTO some_table VALUES ('333 - third  line
444 - fourth line')
/
INSERT INTO some_table VALUES ('555 - fifth  line
                         666 - some ugly line')
/
INSERT INTO some_table VALUES ('123 - meh meh
                         321 - one more
                         678 - and more
                         986 - and more :)')
/

然后让我们进行查询:

    SELECT DISTINCT TRIM(REGEXP_SUBSTR(some_data,'[^'||CHR(10)||']+', 1, level)) 
      FROM some_table
   CONNECT BY REGEXP_SUBSTR(some_data, '[^'||CHR(10)||']+', 1, level) IS NOT NULL;

您可以轻松实现对此的查询。我想有点懒,所以我放了一个DISTINCT来删除重复的ROWS。在正确的代码中,你不应该使用它。

您的数据将如下所示:

333 - third  line
678 - and more
986 - and more :)
321 - one more
555 - fifth  line
444 - fourth line
666 - some ugly line
222 - second line
123 - meh meh
111 - first line