{"id":204337,"date":"2025-05-29T11:26:44","date_gmt":"2025-05-29T03:26:44","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204337\/"},"modified":"2025-05-29T11:26:44","modified_gmt":"2025-05-29T03:26:44","slug":"%e5%a6%82%e4%bd%95%e8%a7%a3%e6%9e%90%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%b1%82%e7%ba%a7%e7%bb%93%e6%9e%84%e9%97%ae%e9%a2%98%ef%bc%9a%e5%a6%82%e4%bd%95%e5%8c%ba%e5%88%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204337\/","title":{"rendered":"\u5982\u4f55\u89e3\u6790\u5b57\u7b26\u4e32\u5c42\u7ea7\u7ed3\u6784\u95ee\u9898\uff1a\u5982\u4f55\u533a\u5206\u5b57\u7b26\u4e32\u4e2d\u4e0d\u540c\u5c42\u7ea7\u7684\u5206\u9694\u7b26\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u89e3\u6790\u5b57\u7b26\u4e32\u5c42\u7ea7\u7ed3\u6784\u95ee\u9898\uff1a\u5982\u4f55\u533a\u5206\u5b57\u7b26\u4e32\u4e2d\u4e0d\u540c\u5c42\u7ea7\u7684\u5206\u9694\u7b26\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u5982\u4f55\u89e3\u6790\u5b57\u7b26\u4e32\u5c42\u7ea7\u7ed3\u6784\u95ee\u9898\uff1a\u5982\u4f55\u533a\u5206\u5b57\u7b26\u4e32\u4e2d\u4e0d\u540c\u5c42\u7ea7\u7684\u5206\u9694\u7b26\uff1f\u300b\uff0c\u8986\u76d6\u4e86\u6587\u7ae0\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241104\/17307255986728c6de96a82.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u89e3\u6790\u5b57\u7b26\u4e32\u5c42\u7ea7\u7ed3\u6784<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><br \/>\u5982\u4f55\u533a\u5206\u5b57\u7b26\u4e32\u4e2d\u4e0d\u540c\u5c42\u7ea7\u7684\u5206\u9694\u7b26\uff0c\u4ee5\u4fbf\u6309\u5c42\u7ea7\u6b63\u786e\u89e3\u6790\u5b57\u7b26\u4e32\uff1f<\/p>\n<p><strong>\u56de\u7b54\uff1a<\/strong><\/p>\n<p><strong>\u786e\u5b9a\u5206\u9694\u7b26\u5c42\u7ea7<\/strong><\/p>\n<ul>\n<li>\u9996\u5148\u9700\u8981\u786e\u5b9a\u5b57\u7b26\u4e32\u4e2d\u5206\u9694\u7b26\u7684\u5c42\u7ea7\u5173\u7cfb\u3002<br \/>\u4f8b\u5982\uff0c\u4e0b\u8ff0\u5b57\u7b26\u4e32\u4e2d\u7684\u7ad6\u7ebf (|) \u6709\u4e0d\u540c\u7684\u5c42\u7ea7\uff1a<\/li>\n<\/ul>\n<pre>str = 'a [ b { c | d } | e ]'<\/pre>\n<ul>\n<li>\u7b2c\u4e00\u4e2a\u7ad6\u7ebf (|) \u5206\u9694 c \u548c d\uff1b<\/li>\n<li>\u7b2c\u4e8c\u4e2a\u7ad6\u7ebf (|) \u5206\u9694 b \u548c e\u3002<\/li>\n<\/ul>\n<p>\u4e3a\u4e86\u8ba9\u7a0b\u5e8f\u8bc6\u522b\u5c42\u7ea7\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<pre># \u5b9a\u4e49\u5b57\u5178\uff0c\u5b58\u653e\u5206\u9694\u7b26\u4f4d\u7f6e\u548c\u5c42\u6b21\nc = {}\n\n# \u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\nn = 0\nfind = 1\n\nwhile n &lt; len(str):\n    # \u8fdb\u5165\u8f83\u9ad8\u5c42\u7ea7\n    if str[n] in (\"[\", \"{\"):\n        find += 1\n    # \u9000\u51fa\u8f83\u4f4e\u5c42\u7ea7\n    if str[n] in (\"]\", \"}\"):\n        find -= 1\n    # \u8bb0\u5f55\u5f53\u524d\u5206\u9694\u7b26\u7684\u4f4d\u7f6e\u548c\u5c42\u6b21\n    if str[n] == \"|\":\n        c.setdefault(find, [n])\n        if n in c[find]:\n            c[find].append(n)\n    n += 1<\/pre>\n<p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<pre>{\n    1: [1],\n    2: [4, 11],\n    3: [7, 8]\n}<\/pre>\n<ul>\n<li>\u952e\u503c\u8868\u793a\u5c42\u7ea7\u3002<\/li>\n<li>\u503c\u8868\u793a\u8be5\u5c42\u7ea7\u4e2d\u5206\u9694\u7b26\u7684\u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n<p><strong>\u89e3\u6790\u5b57\u7b26\u4e32\u5c42\u7ea7<\/strong><\/p>\n<p>\u4e86\u89e3\u5206\u9694\u7b26\u5c42\u7ea7\u540e\uff0c\u5373\u53ef\u9010\u5c42\u89e3\u6790\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre># \u521d\u59cb\u5316\u5b58\u50a8\u7ed3\u679c\u7684\u5217\u8868\nresult = [str]\n\n# \u9010\u5c42\u89e3\u6790\nfor level in sorted(c.keys(), reverse=true):\n    temp = []\n    for pos in c[level]:\n        temp.extend(result.pop().split(str[pos]))\n    result.append(temp)<\/pre>\n<p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<pre>['A', 'B { C | D } | E']\n['A', ['B { C | D }', 'E']]\n['A', ['B', '[ C | D ]', 'E']]\n['A', ['B', ['C', 'D'], 'E']]<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u89e3\u6790\u5b57\u7b26\u4e32\u5c42\u7ea7\u7ed3\u6784\u95ee\u9898\uff1a\u5982\u4f55&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204337","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204337"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204337\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}