【问题标题】:PL/SQL extract numbers between charactersPL/SQL 提取字符之间的数字
【发布时间】:2021-08-30 00:07:27
【问题描述】:

我有一个格式为 12345Q999W12345 的字符串。基本上,一些数字后跟“Q”,然后是更多数字,然后是“W”并以更多数字结尾。我想提取字符“Q”和“W”之间的数字。我能想到的最好的是:

select regexp_substr( '12345Q999W12345' , 'Q[^(\d+)$]+W' ) from dual;

我从上面得到的输出是:

Q999W

关于如何进一步完善这个正则表达式的任何指示?

【问题讨论】:

    标签: sql regex oracle


    【解决方案1】:

    想通了。

    select regexp_substr( '12345Q999W12345' , '\Q(\d+)\W', 1, 1, NULL, 1 ) from dual;
    

    【讨论】:

      【解决方案2】:

      我不确定您想出了什么,因为您的正则表达式(作为答案发布)在我的 19c Oracle 数据库中没有返回任何内容。

      在以下查询中,

      • result - 我的建议(忘记正则表达式;这是一个简单的任务,很容易用好的旧 substr + instr 组合解决)
      • your_1 - 您第一次查询的结果(发布在问题中)
      • your_2 - 第二次查询的结果(作为答案发布)

      SQL> select banner from v$version;
      
      BANNER
      --------------------------------------------------------------------------------
      Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
      SQL> with test (col) as
        2    (select '12345Q999W12345' from dual)
        3  select substr(col,
        4                instr(col, 'Q') + 1,
        5                instr(col, 'W') - instr(col, 'Q') - 1
        6               ) result,
        7               --
        8         regexp_substr(col, 'Q[^(\d+)$]+W') your_1,
        9         regexp_substr(col, '\Q(\d+)\W', 1, 1, NULL, 1) your_2
       10  from test;
      
      RESULT     YOUR_1     YOUR_2
      ---------- ---------- ----------
      999        Q999W
      
      SQL>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-28
        • 2019-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-23
        • 1970-01-01
        相关资源
        最近更新 更多