【问题标题】:How to select a string between the characters如何在字符之间选择一个字符串
【发布时间】:2020-03-27 19:01:31
【问题描述】:

如果我的列(attribute9)包含Pika~Chu~(040)-121-12334~pika78@pika.com 的字段,我该如何提取像

这样的值
 contact = Pika Chu
 phone_nbr = (040)-121-12334
 email = pika78@pika.com

我写过类似的代码

regexp_replace(attribute9, '[^()[:digit:]- ]', '')       phone_nbr,
regexp_substr (attribute9,'[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}') email,
replace(SUBSTR (attribute9 ,0,(INSTR (attribute9 , '(', -1)) - 1),'~',' ')  contact

在 phone_nbr 中,我也得到了电子邮件中的所有数字(即 78)。如何仅在值之间提取(~,~)

【问题讨论】:

  • regexp_replace(attribute9, '[^()[:digit:]-]', '') shipto_phone_nbr, regexp_substr (attribute9,'[A-Za-z0-9._%+-] +@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}') 电子邮件,替换(SUBSTR (attribute9 ,0,(INSTR (attribute9 , '(', - 1)) - 1),'~',' ') shipto_contact,这是代码......为了更好地理解......请帮助我
  • 您的列中的顺序是否始终相同?即首先contact 然后phonenbr 然后email~ 分隔?或者它可以按任何顺序?

标签: sql oracle regexp-replace regexp-substr


【解决方案1】:

您可以使用SUBSTRINSTR 的组合来实现它,如下所示:

SQL> SELECT
  2      REPLACE(SUBSTR(STR, 1, INSTR(STR, '~(') - 1), '~', ' ') AS CONTACT,
  3      SUBSTR(STR, INSTR(STR, '~(') + 1, INSTR(STR, '~', - 1) - INSTR(STR, '~(') - 1) AS PHONE_NBR,
  4      SUBSTR(STR, INSTR(STR, '~', - 1) + 1) AS EMAIL
  5  FROM
  6      (SELECT
  7          'Pika~Chu~(040)-121-12334~pika78@pika.com' AS STR
  8       FROM DUAL);

CONTACT  PHONE_NBR       EMAIL
-------- --------------- ---------------
Pika Chu (040)-121-12334 pika78@pika.com

SQL>

干杯!!

【讨论】:

    【解决方案2】:

    您只能使用REGEXP 来获得如下输出。

    Select 'Pika~Chu~(040)-121-12334~pika78@pika.com' col,
           regexp_substr('Pika~Chu~(040)-121-12334~pika78@pika.com','(.*?)(~)', 1, 1, NULL, 1 ) ||' ' ||regexp_substr('Pika~Chu~(040)-121-12334~pika78@pika.com','(.*?)(~)', 1, 2, NULL, 1 ) contact,
           regexp_substr('Pika~Chu~(040)-121-12334~pika78@pika.com','(.*?)(~)', 1, 3, NULL, 1 ) phone,
           regexp_substr('Pika~Chu~(040)-121-12334~pika78@pika.com','[^~]+$') email
    from dual;
    

    【讨论】:

      【解决方案3】:

      你可以使用regexp_substr():

      select (regexp_substr(str, '^[^~]+', 1) || ' ' || regexp_substr(str, '[^~]+', 1, 2)) as contact,
             regexp_substr(str, '[^~]+', 1, 3) as phone,
             regexp_substr(str, '[^~]+', 1, 4) as email
       from (select 'Pika~Chu~(040)-121-12334~pika78@pika.com' AS STR
             from dual
            ) x;
      

      在上面,调用的唯一区别是最后一个参数,它指定要提取字符串的哪个组件。

      Here 是一个 dbfiddle。

      【讨论】:

        【解决方案4】:

        首先,您应该修复数据库设计,因为在一个列中存储多个值违反了第一范式,只会让人头疼。

        除此之外,使用 REGEXP_SUBSTR() 将字符串拆分为其组件,然后是分隔符或行尾。这样做可以在使用一致的正则表达式的同时处理 NULL 元素。

        with tbl(id, attribute9) as (
          select 1, 'Pika~Chu~(040)-121-12334~pika78@pika.com' from dual union all
          select 2, 'Drowzee~~(040)-121-23445~dz78@pika.com' from dual
        )
        SELECT id, 
          regexp_substr(attribute9, '(.*?)(~|$)', 1, 1, NULL, 1) a9_firstname,
          regexp_substr(attribute9, '(.*?)(~|$)', 1, 2, NULL, 1) a9_lastname,
          regexp_substr(attribute9, '(.*?)(~|$)', 1, 3, NULL, 1) a9_phone,
          regexp_substr(attribute9, '(.*?)(~|$)', 1, 4, NULL, 1) a9_email
        FROM tbl
        order by id;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-12-20
          • 1970-01-01
          • 2013-12-31
          • 1970-01-01
          • 1970-01-01
          • 2021-10-30
          • 1970-01-01
          • 2020-05-12
          相关资源
          最近更新 更多