我想使用字符串中的正则表达式函数在第二个点(。)之前提取文本,例如:

我想从字符串中提取第二个点(.)之前的文本,例如:

Input - XYZ.ABC.MNO.CZ 
Output- XYZ.ABC
Input - AWQ.QA
Output- AWQ.QA
zzzzzadsddsazzzqq 回答:我想使用字符串中的正则表达式函数在第二个点(。)之前提取文本,例如:

看起来你想要除了点以外的任何东西,然后是点,然后是除点之外的任何东西:

with t (v) as (
  select 'XYZ.ABC.MNO.CZ' from dual union all
  select 'AWQ.QA' from dual
)
select regexp_substr(v,'[^\.]+\.[^\.]+') from t;
,

使用 SUBSTR + INSTR 组合(在大型数据集上可能比正则表达式表现更好):

SQL> with test (col) as
  2    (select 'XYZ.ABC.MNO.CZ' from dual union all
  3     select 'AWQ.QA' from dual
  4    )
  5  select col,6         substr(col,1,case when instr(col,'.',2) = 0 then length(col)
  7                             else instr(col,2) - 1
  8                        end
  9               ) result
 10  from test;

COL            RESULT
-------------- --------------
XYZ.ABC.MNO.CZ XYZ.ABC
AWQ.QA         AWQ.QA

SQL>
,

此正则表达式处理点分隔字符串的元素为 NULL 的情况。基本上匹配任何东西,一个文字点,然后是任何后面跟一个文字点或字符串结尾的东西。返回第一组。请注意,如果未找到匹配项,REGEXP_SUBSTR 将返回 NULL(REGEXP_REPLACE 返回原始字符串)。

See this post for more info on why using the REGEX form '[^.]+' does not always work as expected for parsing strings.

WITH T (ID,v) AS (
  SELECT 1,'XYZ.ABC.MNO.CZ' FROM dual UNION ALL
  SELECT 2,'.ABC.MNO.CZ' FROM dual UNION ALL
  SELECT 3,'XYZ..MNO.CZ' FROM dual UNION ALL    
  SELECT 4,'AWQ.QA' FROM dual
)
SELECT ID,REGEXP_SUBSTR(v,'(.*?\..*?)(\.|$)',NULL,1) substring
FROM T
ORDER BY ID;


        ID SUBSTRING     
---------- --------------
         1 XYZ.ABC       
         2 .ABC          
         3 XYZ.          
         4 AWQ.QA        

4 rows selected.

始终使用数据中的意外情况进行测试。

本文链接:https://www.f2er.com/963831.html

大家都在问