【问题标题】:Regular Expression - Retrieve specific asterisk separated value string正则表达式 - 检索特定的星号分隔值字符串
【发布时间】:2014-10-16 16:38:37
【问题描述】:

我需要检索字符串的特定部分,该部分的值由星号分隔

在下面的示例中,我需要检索位于 6 和 7 星号之间的字符串 Client Contact Center Seniors2

我对正则表达式相当陌生,并且只能使用 *[\w]+* 找到两个星号之间的选择值

有没有办法使用正则表达式指定要查看的星号数量,或者有更好的方法来检索我所追求的字符串?

字符串:
2*J25*Owner11*Owner Group2*L231*CLIENTCONTACTCENTRESENIORSQUEUE29*Client Contact Center Seniors2*K20*0*2*C110*SR_STAT_ID2*N18*Referred2*O10*

注意:我将在 Oracle SQL 中使用 REGEXP_LIKE(string, regex) 这个正则表达式。

【问题讨论】:

  • 在中等强大的正则表达式语言中,您可以使用诸如^([^*]*\*){6}([^*]*)\* 之类的符号,其中第一个带括号的表达式匹配 6 个单位的“零个或多个非星号后跟一个星号”,第二个匹配'零个或多个非星号' 和最后的\* 匹配第七个星号。您必须将其调整为 Oracle 实际支持的正则表达式语言。我没有查看它使用哪个正则表达式变体。
  • 谢谢乔纳森,我在 oracle 中使用了以下内容,它从您提供的表达式中返回第二个捕获组 regexp_substr(Audits.audit_log, '^([^*]**){6}([ ^*]*)',1,1,null,2) 你说得对,我正在使用 regexp_substr() 来返回字符串

标签: sql regex oracle


【解决方案1】:

* 是一个正则表达式运算符,需要转义,除非在包含字符列表的括号内使用。您可以使用此简化模式来提取 第七个字。

regexp_substr(Audits.audit_log,'[^*]+',1,7)

SQL Fiddle

查询 1

with x(y) as (
  select '2*J25*Owner11*Owner Group2*L231*CLIENTCONTACTCENTRESENIORSQUEUE29*Client Contact Centre Seniors2*K20*0*2*C110*SR_STAT_ID2*N18*Referred2*O10*'
  from dual
  )
select regexp_substr(y,'([^*]+)\*',1,7,null,1)
from x

Results

| REGEXP_SUBSTR(Y,'([^*]+)\*',1,7,NULL,1) |
|-----------------------------------------|
|          Client Contact Centre Seniors2 |

查询 2

with x(y) as (
  select '2*J25*Owner11*Owner Group2*L231*CLIENTCONTACTCENTRESENIORSQUEUE29*Client Contact Centre Seniors2*K20*0*2*C110*SR_STAT_ID2*N18*Referred2*O10*'
  from dual
  )
select regexp_substr(y,'[^*]+',1,7)
from x

Results

|   REGEXP_SUBSTR(Y,'[^*]+',1,7) |
|--------------------------------|
| Client Contact Centre Seniors2 |

【讨论】:

    【解决方案2】:

    您也可以为此使用INSTRSUBSTR。简单快速,但不如REGEXP_SUBSTR简洁。

    with t as (   
      select '2*J25*Owner11*Owner Group2*L231*CLIENTCONTACTCENTRESENIORSQUEUE29*Client Contact Centre Seniors2*K20*0*2*C110*SR_STAT_ID2*N18*Referred2*O10*' testvalue
      from dual
    )
    select substr(testvalue, instr(testvalue, '*', 1, 6)+1, instr(testvalue, '*', 1, 7) - instr(testvalue, '*', 1, 6) - 1)
    from t;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多