【问题标题】:Extracting words from a sentence in oracle在oracle中从句子中提取单词
【发布时间】:2021-02-04 08:45:52
【问题描述】:

我有表格和'test'表格,它有列文件名,其中包含不同格式的句子。

filename                                                
LA West Employer us Rort October 2015.txt201510         
LA loyer sus Rrt April 2017         
LA oyer sus Rept April 2018.txt201712               
LA oyer sus Ret April 2019.txt201712                
LA Eoyer sus Ret Aug 2019.txt201712             
LA oyer sus Rort August 2017(2).txt201708           
LA Eyer sus Rort August 2018 (1).txt201712      
LA Eyer sus Reort Dec 2017.txt201711                
LA Emyer sus Report Dec 2018 (1).txt201712  
LA Emyer sus Report October- 2018 (1).txt201712     

我的预期输出是:

October 2015
April 2017
Aug 2019
Dec 2017

每一行都包含月份年份,我想从中提取那部分。

我尝试如下:

SELECT
SubStr(filename,INSTR(filename,'Report')+7,(INSTR(filename,'(')-1)-(INSTR(filename,'Report')+7))res
FROM test ;

select regexp_substr(filename, '[^[:space:]]+[[:space:]][^[:space:]]+$') from table;

它没有工作。我怎样才能达到这个结果?

【问题讨论】:

    标签: oracle oracle12c


    【解决方案1】:

    一个可能的解决方案是:

    select replace(regexp_substr('LLC Emyer sus Report October- 2018 (1).txt201712', '[[:alpha:]-]+[[:space:]][[:digit:]]{4}'), '-', '')
      from dual;
    

    【讨论】:

    • 试试这个版本:replace(regexp_substr('LLC Emyer sus Report October- 2018 (1).txt201712', '[[:alpha:]-]+[[:space:]][[ :digit:]]{4}'), '-', '')
    • 好的,谢谢你的代码为我工作,你能解释一下这些正则表达式的作用吗?
    • 表达式查找 1 个或多个字母或 - 字符,在此之后有一个空格字符,最后是 4 个数字。
    【解决方案2】:

    这个模式也很好用

    select FILENAME, regexp_substr(FILENAME, '([[:alpha:]]+\s+[[:digit:]]+)\s*(\([[:digit:]]+\))?\.txt.+?$', 1, 1, 'i', 1) expected_output
    from your_table
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多