{"id":7058,"date":"2021-11-03T14:16:08","date_gmt":"2021-11-03T06:16:08","guid":{"rendered":"https:\/\/nj.transwarp.cn:8180\/?p=7058"},"modified":"2025-02-26T14:57:14","modified_gmt":"2025-02-26T06:57:14","slug":"%e5%85%b3%e4%ba%8einceptor%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0oracle%e7%9a%84regexp_substr%e5%92%8cconnect-by-level","status":"publish","type":"post","link":"https:\/\/kbwp.transwarp.cn\/?p=7058","title":{"rendered":"\u5173\u4e8einceptor\u4e2d\u5982\u4f55\u5b9e\u73b0oracle\u7684regexp_substr\u548cconnect by level"},"content":{"rendered":"<h3>\u6982\u8981\u63cf\u8ff0<\/h3>\n<hr \/>\n<p>\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\uff0c\u5728inceptor\u4e2d\uff0c\u5982\u4f55\u5b9e\u73b0oracle\u7684<code>regexp_substr<\/code>\u548c<code>connect by level<\/code>\u83b7\u53d6\u62c6\u5206\u5b57\u7b26\u3002<\/p>\n<p>\u9996\u5148\u4f1a\u4eceoracle\u7684\u89d2\u5ea6\u89e3\u6790\u4e0b\u8fd9\u4e2a\u7528\u6cd5\u662f\u5982\u4f55\u9010\u6b65\u5b9e\u73b0\u7684\uff0c\u8fdb\u800c\u5c55\u5f00\u5230inceptor\u7684\u6539\u5199\u65b9\u6cd5\u3002<\/p>\n<h3>\u8be6\u7ec6\u8bf4\u660e<\/h3>\n<hr \/>\n<h4>\u4e00\u3001oracle\u4e2d\u7684\u5b9e\u73b0<\/h4>\n<pre><code class=\"language-ruby\">REGEXP_SUBSTR \u51fd\u6570\u683c\u5f0f\u5982\u4e0b\uff1a\n    function REGEXP_SUBSTR(String, pattern, position, occurrence, modifier)\n    __srcstr     \uff1a\u9700\u8981\u8fdb\u884c\u6b63\u5219\u5904\u7406\u7684\u5b57\u7b26\u4e32\n    __pattern    \uff1a\u8fdb\u884c\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\n    __position   \uff1a\u8d77\u59cb\u4f4d\u7f6e\uff0c\u4ece\u7b2c\u51e0\u4e2a\u5b57\u7b26\u5f00\u59cb\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\uff08\u9ed8\u8ba4\u4e3a1\uff09\n    __occurrence \uff1a\u6807\u8bc6\u7b2c\u51e0\u4e2a\u5339\u914d\u7ec4\uff0c\u9ed8\u8ba4\u4e3a1\n    __modifier   \uff1a\u6a21\u5f0f\uff08'i'\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u8fdb\u884c\u68c0\u7d22\uff1b'c'\u533a\u5206\u5927\u5c0f\u5199\u8fdb\u884c\u68c0\u7d22\u3002\u9ed8\u8ba4\u4e3a'c'\u3002\uff09<\/code><\/pre>\n<p>\u5b9e\u9645\u5e94\u7528\u5982\u4e0b\uff1a\u5728oracle\u4e2d\uff0c\u4f7f\u7528\u4e00\u6761\u8bed\u53e5\u5b9e\u73b0\u5c06&#8217;34,56,-23&#8217;\u62c6\u5206\u6210&#8217;34&#8217;,&#8217;56&#8217;,&#8217;-23&#8217;\u7684\u96c6\u5408\u3002<\/p>\n<pre><code class=\"language-sql\">--1\u3001\u67e5\u8be2\u4f7f\u7528\u6b63\u5219\u5206\u5272\u540e\u7684\u7b2c\u4e00\u4e2a\u503c\uff0c\u4e5f\u5c31\u662f34\n> SELECT REGEXP_SUBSTR('34,56,-23','[^,]+',1,1,'i') AS STR FROM DUAL;\n--\u7ed3\u679c\u662f\uff1a34\n\n--2\u3001\u67e5\u8be2\u4f7f\u7528\u6b63\u5219\u5206\u5272\u540e\u7684\u6700\u540e\u4e00\u4e2a\u503c\uff0c\u4e5f\u5c31\u662f-23\n> SELECT REGEXP_SUBSTR('34,56,-23','[^,]+',1,3,'i') AS STR FROM DUAL;\n--\u7ed3\u679c\u662f\uff1a-23\n\n--3\u3001\u83b7\u53d6\u4e00\u4e2a\u591a\u4e2a\u6570\u503c\u7684\u5217\uff0c\u4ece\u800c\u80fd\u591f\u8ba9\u7ed3\u679c\u4ee5\u591a\u884c\u7684\u5f62\u5f0f\u5c55\u793a\u51fa\u6765\n> SELECT LEVEL FROM DUAL CONNECT BY LEVEL <=7;\n--\u7ed3\u679c\u662f\uff1a\n--1\n--2\n--3\n--4\n--5\n--6\n--7\n\n--4\u3001\u5c06\u4e0a\u9762REGEXP_SUBSTR\u7684occurrence\u5173\u8054\n> SELECT NVL(REGEXP_SUBSTR('34,56,-23', '[^,]+', 1, LEVEL, 'i'), 'NULLL') AS STR FROM DUAL CONNECT BY LEVEL <= 7;\n--\u7ed3\u679c\u662f\uff1a\n--34\n--56\n---23\n--null\n--null\n--mull\n--null\n\n--5\u3001\u4f18\u5316\u4e0a\u9762\u7684SQL\u8bed\u53e5\uff0c\u8ba9\u751f\u6210\u7684\u884c\u7684\u6570\u91cf\u7b26\u5408\u5b9e\u9645\u60c5\u51b5\n> SELECT REGEXP_SUBSTR('34,56,-23', '[^,]+', 1, LEVEL, 'i') AS STR FROM DUAL \\\n> CONNECT BY LEVEL <= LENGTH('34,56,-23') - LENGTH(REGEXP_REPLACE('34,56,-23', ',', '')) + 1;\n--\u7ed3\u679c\u662f\uff1a\n--34\n--56\n---23<\/code><\/pre>\n<h4>\u4e8c\u3001inceptor\u4e2d\u7684\u5b9e\u73b0<\/h4>\n<pre><code class=\"language-ruby\">REGEX_SUBSTR \u51fd\u6570\u683c\u5f0f\u5982\u4e0b\uff1a\n    function regex_substr (<source>, <regex> [, <pos>[, <occurrence> [, <match_parameter> ] ]])\n\n    # \u8be5\u51fd\u6570\u9700\u8981\u81f3\u5c11\u4e24\u4e2a\u51fd\u6570\uff1a\u4e00\u6bb5\u6587\u672c <source>\u548c\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f <regex>\u3002\n    # \u8be5\u51fd\u6570\u4f1a\u5c06 <source>\u4e2d\u548c <regex> \u7b2c\u4e00\u4e2a\u53d1\u751f\u5339\u914d\u7684\u5b50\u5b57\u7b26\u4e32\u8fd4\u56de\u3002\n    # <pos> \u662f\u4e00\u4e2a\u6b63\u6574\u6570\uff0c\u7528\u4e8e\u6307\u5b9a\u4ece <source>\u4e2d\u7684\u7b2c\u51e0\u4e2a\u5b57\u7b26\u5f00\u59cb\u5339\u914d\u3002 \n    # <occurrence>\u662f\u4e00\u4e2a\u6b63\u6574\u6570\uff0c\u7528\u4e8e\u6307\u5b9a\u8fd4\u56de\u7b2c\u51e0\u4e2a\u53d1\u751f\u5339\u914d\u7684\u5b50\u5b57\u7b26\u4e32\u3002\n    # <match_parameter> \u7528\u4e8e\u6307\u5b9a\u5339\u914d\u65b9\u5f0f\uff0c\n        i\uff1a\u5927\u5c0f\u5199\u4e0d\u654f\u611f\uff1b\n        c\uff1a\u5927\u5c0f\u5199\u654f\u611f\uff1b\n        n \u5c06 .\uff08\u901a\u914d\u7b26\uff09\u548c\u6362\u884c\u7b26\u5339\u914d\uff0c\u9ed8\u8ba4\u4e3a\u4e0d\u5339\u914d\uff1b\n        m \u5c06 <source> \u4f5c\u4e3a\u591a\u884c\u6765\u5904\u7406\uff0c\u5c06 ^ \u4f5c\u4e3a\u884c\u5f00\u5934\uff0c$\u4f5c\u4e3a\u884c\u7ed3\u5c3e\uff1b \n        x \u5ffd\u7565\u7a7a\u683c\u3002<\/code><\/pre>\n<p><font color=red> \u6ce8\u610f\uff1ainceptor\u4e2d\u662fregex_substr\uff0coracle\u4e2d\u662fregexp_substr\uff0c\u4e24\u8005\u5dee\u4e86\u4e00\u4e2a\u5b57\u6bcdp <\/font><\/p>\n<p>\u5148\u8bf4\u7ed3\u8bba\uff0c\u6539\u5199\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-sql\">SELECT\n    REGEX_SUBSTR('34,56,-23', '[^,]+', 1, pos + 1, 'i') AS STR\nFROM\n    system.dual\n    lateral view posexplode(split('34,56,-23', ',')) t AS\n    pos, val;<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/11\/image-1635920048112.png\" alt=\"file\" \/><\/p>\n<blockquote>\n<p>\u5728\u9ad8\u7248\u672c\u4e0b\uff0c\u8be5\u8bed\u53e5\u53ef\u4ee5\u6b63\u5e38\u6267\u884c\uff0c\u4f46\u5728\u67d0\u4e9b\u4f4e\u7248\u672c\u4e0b\uff0c\u9700\u8981 <strong>\u8054\u7cfb\u7814\u53d1\u6362\u5305\uff08\u53c2\u8003 <a href=\"https:\/\/jira.transwarp.io\/browse\/SLA-10226\" title=\"SLA-10226\">SLA-10226<\/a>\uff09<\/strong> \u4f18\u5148\u89e3\u51b3position\u7684\u95ee\u9898\u3002<br \/>\n\u53c2\u8003 <a href=\"https:\/\/jira.transwarp.io\/browse\/WARP-50953\" title=\"WARP-50953\">WARP-50953<\/a>\u3002<strong>\u539f\u56e0\u662foracle\u9ed8\u8ba4position\u662f\u4ece1\u5f00\u59cb\u7684, \u800cinceptor\u662f\u4ece0\u5f00\u59cb\u7684\u3002<\/strong><br \/>\n\u9a8c\u8bc1sql\uff0c\u7ed3\u679c\u4e3a\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e3234\uff0c\u5219\u6b63\u5e38\uff1b\u4e3a56\uff0c\u5219\u5931\u8d25\u3002\uff1a<\/p>\n<pre><code class=\"language-sql\">SELECT REGEX_SUBSTR('34,56,-23', '[^,]+', 1, 1, 'i') AS STR  FROM system.dual;<\/code><\/pre>\n<\/blockquote>\n<p><strong>\u9644\uff1a<code>connect by level<\/code>\u7684\u6539\u5199<\/strong><\/p>\n<pre><code class=\"language-sql\">--oracle:\nSELECT LEVEL LV FROM DUAL CONNECT BY LEVEL <= 100<\/code><\/pre>\n<pre><code class=\"language-sql\">--\u65b9\u6848\u4e00\uff1a\nSELECT generate_series(1,10);\n\n--\u65b9\u6848\u4e8c\uff1a\nSET character.literal.as.string=TRUE;\nSELECT\n    t.pos+1\nFROM\n    system.dual\n    lateral view posexplode(split(space(100), '')) t AS\n    pos,val;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981\u63cf\u8ff0 \u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\uff0c\u5728inceptor\u4e2d\uff0c\u5982\u4f55\u5b9e\u73b0oracle\u7684regexp_substr\u548cconnect ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/kbwp.transwarp.cn\/?p=7058\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-7058","post","type-post","status-publish","format-standard","hentry","category-sql_issue"],"acf":[],"_links":{"self":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/7058","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7058"}],"version-history":[{"count":4,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/7058\/revisions"}],"predecessor-version":[{"id":15943,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/7058\/revisions\/15943"}],"wp:attachment":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}