3

我正在尝试使用 regexp_subtr 拆分字符串,但我无法使其工作。

所以,首先,我有这个查询

select regexp_substr('Helloworld - test!' ,'[[:space:]]-[[:space:]]') from dual

很好地提取了我的分隔符 -空白-空白

但是,当我尝试使用此选项拆分字符串时,它就不起作用了。

select regexp_substr('Helloworld - test!' ,'[^[[:space:]]-[[:space:]]]+')from dual

查询不返回任何内容。

帮助将不胜感激!谢谢

4

5 回答 5

9

SQL小提琴

Oracle 11g R2 模式设置

CREATE TABLE TEST( str ) AS
          SELECT 'Hello world - test-test! - test' FROM DUAL
UNION ALL SELECT 'Hello world2 - test2 - test-test2' FROM DUAL;

查询 1

SELECT Str,
       COLUMN_VALUE AS Occurrence,
       REGEXP_SUBSTR( str ,'(.*?)([[:space:]]-[[:space:]]|$)', 1, COLUMN_VALUE, NULL, 1 ) AS split_value
FROM   TEST,
       TABLE(
         CAST(
           MULTISET(
             SELECT LEVEL
             FROM   DUAL
             CONNECT BY LEVEL < REGEXP_COUNT( str ,'(.*?)([[:space:]]-[[:space:]]|$)' )
           )
           AS SYS.ODCINUMBERLIST
         )
       )

结果

|                               STR | OCCURRENCE |  SPLIT_VALUE |
|-----------------------------------|------------|--------------|
|   Hello world - test-test! - test |          1 |  Hello world |
|   Hello world - test-test! - test |          2 |   test-test! |
|   Hello world - test-test! - test |          3 |         test |
| Hello world2 - test2 - test-test2 |          1 | Hello world2 |
| Hello world2 - test2 - test-test2 |          2 |        test2 |
| Hello world2 - test2 - test-test2 |          3 |   test-test2 |
于 2015-07-27T15:11:15.900 回答
1

如果我理解正确,这将对您有所帮助。目前你得到的输出是Helloworld(最后有空格)。所以我假设你不想在最后有空间。如果是这样,您也可以简单地使用分隔符中的空格。

select regexp_substr('Helloworld - test!' ,'[^ - ]+',1,1)from dual;

OUTPUT
Helloworld(No space at the end)

正如你在评论中提到的,如果你想要两列输出Helloworldtest!。您可以执行以下操作。

select regexp_substr('Helloworld - test!' ,'[^ - ]+',1,1),
       regexp_substr('Helloworld - test!' ,'[^ - ]+',1,3) from dual;

OUTPUT
col1         col2
Helloworld   test!
于 2015-07-27T14:09:31.640 回答
1

试图'[[:space:]]-[[:space:]]'通过将匹配字符串放入带有抑扬符 (^) 的字符类来否定匹配字符串来否定它是行不通的。一对方括号之间的所有内容都被视为可选单个字符的列表,但命名的命名字符类除外,它们扩展为可选字符列表,但是,由于字符类的嵌套方式,您的外括号很可能是解释如下:

  • [^[[:space:]]单个非空格非左方括号字符
  • -后跟一个连字符
  • [[:space:]]后跟一个空格字符
  • ]+后跟 1 个或多个右方括号。

使用 regexp_replace 将多字符分隔符转换为单个字符可能更容易,然后使用 regex_substr 来查找各个部分:

select regexp_substr(regexp_replace('Helloworld - test!'
                                   ,'[[:space:]]-[[:space:]]'
                                   ,chr(11))
                    ,'([^'||chr(11)||']*)('||chr(11)||'|$)'
                    ,1 -- Start here
                    ,2 -- return 1st, 2nd, 3rd, etc. match
                    ,null
                    ,1 -- return 1st sub exp
                    )
  from dual;

在这段代码中,我首先更改-chr(11). 这是 ASCII 垂直制表符 (VT) 字符,它不太可能出现在大多数文本字符串中。然后 regexp_substr 的匹配表达式匹配所有非 VT 字符,后跟一个 VT 字符或行尾。仅返回非 VT 字符(第一个子表达式)。

于 2015-07-27T14:58:04.230 回答
0

对 MT0 的回答略有改进。使用 regexp_count 的动态计数并证明它可以处理 [^delimiter]+ 作为模式的格式不处理 NULL 列表元素的空值。更多信息:Split comma separated values to columns

SQL> with tbl(str) as (
  2    select ' - Hello world - test-test! -  - test - ' from dual
  3  )
  4  SELECT LEVEL AS Occurrence,
  5         REGEXP_SUBSTR( str ,'(.*?)([[:space:]]-[[:space:]]|$)', 1, LEVEL, NULL, 1 ) AS split_value
  6  FROM   tbl
  7  CONNECT BY LEVEL <= regexp_count(str, '[[:space:]]-[[:space:]]')+1;

OCCURRENCE SPLIT_VALUE
---------- ----------------------------------------
         1
         2 Hello world
         3 test-test!
         4
         5 test
         6

6 rows selected.

SQL>
于 2015-07-27T15:54:04.257 回答
0
CREATE OR REPLACE FUNCTION field(i_string            VARCHAR2
                                ,i_delimiter         VARCHAR2
                                ,i_occurance         NUMBER
                                ,i_return_number     NUMBER DEFAULT 0
                                ,i_replace_delimiter VARCHAR2) RETURN VARCHAR2     IS
  -----------------------------------------------------------------------
  -- Function Name.......: FIELD
  -- Author..............: Dan Simson
  -- Date................: 05/06/2016 
  -- Description.........: This function is similar to the one I used from 
  --                       long ago by Prime Computer.  You can easily
  --                       parse a delimited string.
  -- Example.............: 
  --  String.............: This is a cool function
  --  Delimiter..........: ' '
  --  Occurance..........: 2
  --  Return Number......: 3
  --  Replace Delimiter..: '/'
  --  Return Value.......: is/a/cool
  --------------------------------------------------------------------------    ---                                    
  v_return_string  VARCHAR2(32767);
  n_start          NUMBER := i_occurance;
  v_delimiter      VARCHAR2(1);
  n_return_number  NUMBER := i_return_number;
  n_max_delimiters NUMBER := regexp_count(i_string, i_delimiter);
BEGIN
  IF i_return_number > n_max_delimiters THEN
    n_return_number := n_max_delimiters + 1;
  END IF;
  FOR a IN 1 .. n_return_number LOOP
    v_return_string := v_return_string || v_delimiter || regexp_substr    (i_string, '[^' || i_delimiter || ']+', 1, n_start);
    n_start         := n_start + 1;
    v_delimiter     := nvl(i_replace_delimiter, i_delimiter);
  END LOOP;
  RETURN(v_return_string);
END field;


SELECT field('This is a cool function',' ',2,3,'/') FROM dual;

SELECT regexp_substr('This is a cool function', '[^ ]+', 1, 1) Word1
      ,regexp_substr('This is a cool function', '[^ ]+', 1, 2) Word2
      ,regexp_substr('This is a cool function', '[^ ]+', 1, 3) Word3
      ,regexp_substr('This is a cool function', '[^ ]+', 1, 4) Word4
      ,regexp_substr('This is a cool function', '[^ ]+', 1, 5) Word5
  FROM dual;
于 2016-05-06T17:00:33.443 回答