在制表符分隔的记录上使用regexp_substr,字段中有空格

时间:2018-10-15 17:31:55

标签: regex oracle12c regexp-substr

使用Oracle 12c,如何使用 regexp_substr 分隔制表符分隔的记录,其字段中可能包含空格?该记录有四个字段。第三个字段包含带空格的单词。

我以此为参考:Oracle Regex

这是我的查询

with tab_delimited_record as
 (select 'Field1    Field2  This is field3 and contains spaces  Field4' as record_with_fields from dual) 
 select record_with_fields,
        regexp_substr('\S+',1,3) as field3a, -- Expect ==>This is field3...
        regexp_substr('\t+',1,3) as field3b, -- Expect==>This is field3...
        regexp_substr('[[::space::]]+',1,3) as field_3c -- Another version
  from  tab_delimited_record

所需结果

RECORD_WITH_FIELDS

Field1 Field2这是field3,其中包含空格Field4

FIELD3

这是field3,其中包含空格

3 个答案:

答案 0 :(得分:1)

我相信您正在寻找类似的东西。请注意,此示例出于示例的目的返回了所有字段,但是如果您只需要选择field3,则当然可以。 CTE构建带有制表符分隔字段的字符串。然后,查询使用regex_substr来获取第n个(第4个参数)字符串,后跟TAB或行尾。

2
6
24
120

答案 1 :(得分:1)

在使用Oracle SQL时,不能从字面上使'\ t'无效。您需要断开字符串,使用chr(09)(ascii选项卡),然后构造字符串。试试看

with tab_delimited_record as
 (select 'Field1'||chr(09)||'Field2'||chr(09)||'This is field3 and contains spaces'||chr(09)||'Field4' as record_with_fields from dual) 
    select record_with_fields,
        regexp_substr(record_with_fields,'(\S+)\s+(\S+)\s+(.+)\s+',1,1,'',3) as field3a, -- Expect ==>This is field3...
        regexp_substr(record_with_fields,'(\S+)'||chr(09)||'(\S+)'||chr(09)||'(.+)\s+',1,1,'',3) as field3b, -- Expect==>This is field3...
        regexp_substr(record_with_fields,'(\S+)[[:space:]]+(\S+)[[:space:]]+(.+)[[:space:]]+',1,1,'',3) as field_3c -- Another version
  from  tab_delimited_record

答案 2 :(得分:1)

正则表达式的另一个版本:

with tab_delimited_record(record_with_fields) as (
  select 'Field1'||chr(09)||'Field2'||chr(09)||'This is field3 and contains spaces'||chr(09)||'Field4' from dual
) 
select record_with_fields,
       regexp_substr(record_with_fields, '[^'||chr(09)||']+', 1, 3) as field_3
from  tab_delimited_record;