首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >拆分字符串,并将子字符串保存在数组中。

拆分字符串,并将子字符串保存在数组中。
EN

Stack Overflow用户
提问于 2019-02-22 10:15:53
回答 2查看 323关注 0票数 1

我有一根绳子是这样的:

代码语言:javascript
复制
(InstrTyp EQ DebtInstruments) AND (IntrnlTrdTyp EQ IntraGrpBP) AND (Entity EQ GSSTH)

我想将它们保存在一个数组中,以便:

  • 第一要素:InstrTyp EQ DebtInstruments
  • 第二要素:IntrnlTrdTyp EQ IntraGrpBP
  • 第三要素:Entity EQ GSSTH

新的PL/SQL,欣赏一个详细的答案。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-22 10:33:00

您可以使用regexp_substr提取括号之间的字符串。

代码语言:javascript
复制
DECLARE
     TYPE string_array_typ IS
          TABLE OF VARCHAR2(100);
     split_strs       string_array_typ := string_array_typ(); --define and declare an array of string
     l_str_to_split   VARCHAR2(1000) := '(InstrTyp EQ DebtInstruments) AND (IntrnlTrdTyp EQ IntraGrpBP) AND (Entity EQ GSSTH)'
     ;
BEGIN 
FOR i IN 1..regexp_count(l_str_to_split,'\(.*?\)')  --loop until as many number of strings between `()`
  LOOP 
   split_strs.extend;
   split_strs(i) := regexp_substr(l_str_to_split,'\((.*?)\)',1,i,NULL,1); -- Assign ith element to ith word between `()`
 END loop;

 FOR i IN 1..split_strs.count LOOP
     dbms_output.put_line(split_strs(i) ); --display the contents of the array
  END LOOP;
END;
/
票数 2
EN

Stack Overflow用户

发布于 2019-02-22 10:24:44

我们可以分两步来做。首先,从输入字符串中删除所有括号,然后对模式\s*AND\s*上的数组执行regex拆分。

代码语言:javascript
复制
select
    regexp_split_to_array(regexp_replace(txt, '[()]', '', 'g'), '\s*AND\s*')
from your_table;

Demo

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54824817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档