{"id":207087,"date":"2025-07-08T09:17:44","date_gmt":"2025-07-08T01:17:44","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207087\/"},"modified":"2025-07-08T09:17:44","modified_gmt":"2025-07-08T01:17:44","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8%e5%ad%97%e7%ac%a6%e4%b9%8b%e9%97%b4%e6%9f%a5%e6%89%be%e5%92%8c%e6%9b%bf%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207087\/","title":{"rendered":"\u5982\u4f55\u5728\u5b57\u7b26\u4e4b\u95f4\u67e5\u627e\u548c\u66ff\u6362"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5982\u4f55\u5728\u5b57\u7b26\u4e4b\u95f4\u67e5\u627e\u548c\u66ff\u6362<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 22:36:38<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728\u5b57\u7b26\u4e4b\u95f4\u67e5\u627e\u548c\u66ff\u6362\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u5c06\u4ee5\u4e0b\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e8c\u4e2a\u201cAND\u201d\u66ff\u6362\u4e3a\u201cOR\u201d\uff1a<\/p>\n<p><code>country == \"BR\" AND (leftHour &gt;= 6 AND rightHour &lt; 24)<\/code><\/p>\n<p>\u6211\u7684\u7b56\u7565\u662f\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6267\u884c\u6b64\u64cd\u4f5c\uff1a<code>\\((.*)\\)<\/code> \u4f46\u8fd9\u5339\u914d\u62ec\u53f7\u4e4b\u95f4\u7684\u6240\u6709\u5b57\u7b26\uff0c\u6211\u53ea\u60f3\u5339\u914d\u4e24\u4e2a\u62ec\u53f7\u4e4b\u95f4\u7684 <code>AND<\/code> \u3002<\/p>\n<p>\u5728 Go \u4e2d\u5c31\u5f88\u5b8c\u7f8e\uff0c\u4f46\u597d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5c31\u8db3\u591f\u4e86\u3002<\/p>\n<p>\u7f16\u8f91\uff1a\u5b57\u7b26\u4e32\u4e0d\u662f\u56fa\u5b9a\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u524d\u9762\u6709\u591a\u4e2a\u201cAND\u201d\uff0c\u4f8b\u5982\uff1a <code>currency == \"USD\" AND \u56fd\u5bb6 == \"BR\" AND (leftHour &gt;= 6 AND rightHour &lt; 24)<\/code><\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u50cf <code>\\([^()]*\\)<\/code> \u8fd9\u6837\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u6700\u91cc\u9762\u62ec\u53f7\u5185\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u4f7f\u7528 \u4ec5\u5c06\u5339\u914d\u6587\u672c\u4e2d\u7684\u6240\u6709 <code>and<\/code> \u66ff\u6362\u4e3a <code>or<\/code>\uff1a<\/p>\n<pre>package main\n\nimport (\n        \"fmt\"\n        \"strings\"\n        \"regexp\"\n)\n\nfunc main() {\n        input := `country == \"br\" and (lefthour &gt;= 6 and righthour &lt; 24)`\n        r := regexp.mustcompile(`\\([^()]*\\)`)\n        fmt.println(r.replaceallstringfunc(input, func(m string) string {\n                return strings.replaceall(m, \"and\", \"or\")\n        }))\n}<\/pre>\n<p>\u67e5\u770b<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e8c\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362 <code>and<\/code>\uff1a<\/p>\n<pre>return regexp.MustCompile(`\\bAND\\b`).ReplaceAllString(m, \"OR\")<\/pre>\n<p>\u8fd9\u5c06\u66ff\u6362\u6574\u4e2a\u5355\u8bcd\u7684 <code>and<\/code>\uff0c\u65e0\u8bba\u662f\u5426\u6709\u7a7a\u683c\u3002\u8bf7\u53c2\u9605 \u3002<\/p>\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\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#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-207087","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207087","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=207087"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207087\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}