如何在oracle中的特定模式之后获取子字符串

时间:2015-06-24 06:09:57

标签: oracle

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CustomerDebitCardLinkingWSBean>
<agentMsisdn>+923326088024</agentMsisdn>
<cardNumber>6233070000588011</cardNumber>
<channel>USSD</channel>
<customerMobileNumber>03345310550</customerMobileNumber>
<fromAgent>true</fromAgent>
<status>false</status>
</CustomerDebitCardLinkingWSBean>

这是一个字符串,现在我想得到频道标签形式的子字符串从头到尾怎么可能?

1 个答案:

答案 0 :(得分:0)

如果您不想使用xml解析器,则可以使用regexp_replace

declare

  c varchar2(4000) := q'{
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CustomerDebitCardLinkingWSBean>
<agentMsisdn>+923326088024</agentMsisdn>
<cardNumber>6233070000588011</cardNumber>
<channel>USSD</channel>
<customerMobileNumber>03345310550</customerMobileNumber>
<fromAgent>true</fromAgent>
<status>false</status>
</CustomerDebitCardLinkingWSBean>
  }';

begin


  dbms_output.put_line(

      regexp_replace( c, 
        '.*<channel>(.*)</channel>.*',
        '\1',
        1,
        1,
       'n'
      )

  );

end;

虽然我强烈反对这种用法的建议。

为了完整起见,这是一个用Oracle XMLType做到的解决方案:

declare

  xml XMLType := XMLType(q'{
<CustomerDebitCardLinkingWSBean>
<agentMsisdn>+923326088024</agentMsisdn>
<cardNumber>6233070000588011</cardNumber>
<channel>USSD</channel>
<customerMobileNumber>03345310550</customerMobileNumber>
<fromAgent>true</fromAgent>
<status>false</status>
</CustomerDebitCardLinkingWSBean>
  }');


  channel varchar2(20);

begin

  select extractvalue(xml, 'CustomerDebitCardLinkingWSBean/channel') into channel from dual;

  dbms_output.put_line(channel);

end;
/