{"id":2896,"date":"2018-06-24T08:41:04","date_gmt":"2018-06-24T00:41:04","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2896"},"modified":"2018-06-24T08:41:04","modified_gmt":"2018-06-24T00:41:04","slug":"go%e8%af%ad%e8%a8%80%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2896","title":{"rendered":"go\u8bed\u8a00\u6b63\u5219\u8868\u8fbe\u5f0f\u6559\u7a0b"},"content":{"rendered":"<p>\n\t\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u4e2a\u7ecf\u5e38\u4f1a\u4f7f\u7528\u7684\u529f\u80fd\uff0c\u5c24\u5176\u662f\u722c\u866b\u5e94\u7528\u4e2d\u3002java \u4e2d\u6709\u5bf9\u5e94\u7684\u9009\u62e9\u5668\uff0cgo \u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u81ea\u5df1\u5c01\u88c5\uff0c\u4e5f\u53ef\u4ee5\u81ea\u5df1\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u76f8\u5173\u5185\u5bb9\u3002\n<\/p>\n<p>\n\t\u5728 go \u8bed\u8a00\u4e2d\uff0c\u5229\u7528 goquery \u548c xpath \u7b2c\u4e09\u65b9\u5e93\u5339\u914d\u6709\u7528\u4fe1\u606f\u3002\u8fd9\u4e9b\u5185\u5bb9\u6211\u4eec\u4e0d\u8bb2\uff0c\u672c\u6587\u4e3b\u8981\u5b66\u4e60\u66f4\u4f18\u96c5\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u3002\u4e0b\u6765\u5927\u6982\u4ecb\u7ecd\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\n<\/p>\n<p>\n\t\u6bd4\u5982\u6211\u4eec\u5339\u914d\u73cd\u7231\u7f51\u57ce\u5e02\u5217\u8868\u7684\u65f6\u5019\uff0c\u4f1a\u53d6\u5339\u914d\u6240\u6709\u57ce\u5e02\u7684url\uff0c\u5982\u4e0b\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"goquery\" src=\"http:\/\/ocuzluml6.bkt.clouddn.com\/20180624083337.png\" style=\"width: 662px;height: 402px\" \/>\n<\/p>\n<p>\n\t\u53ef\u4ee5\u770b\u5230\u56fe\u7247\u540e\u662f\u5c0f\u5199\u5b57\u6bcd\u52a0\u6570\u5b57\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u7528\u4ee5\u4e0b\u65b9\u5f0f\u63d0\u53d6\uff1a\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;a href=&quot;(http:\/\/www.zhenai.com\/zhenghun\/[0-9a-z]+)\"[^&gt;]*&gt;([^&lt;]+)&lt;\/a&gt;<\/pre>\n<p>\n\t[0-9a-z]+\u8868\u793a\u5339\u914d\u5c0f\u5199\u5b57\u6bcd\u6216\u8005\u6570\u5b57\u81f3\u5c11\u4e00\u6b21\uff0c[^&gt;]*\u8868\u793a\u5339\u914d\u975e&gt;\u7684\u5b57\u7b26\u4efb\u610f\u6b21\uff0c\u7136\u540e[^&lt;]+\u8868\u793a\u5339\u914d\u975e&lt;\u5b57\u7b26\u81f3\u5c11\u4e00\u6b21\u3002\u6211\u4eec\u8981\u53d6\u5230\u57ce\u5e02\u7684url\u548c\u57ce\u5e02\u540d\uff0c\u6240\u4ee5\u5bf9\u8fdb\u884c\u4e86\u5206\u7ec4\u3002\n<\/p>\n<p>\n\t\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5c31\u53ef\u4ee5\u62ff\u5230url\u548ccity\u3002\n<\/p>\n<pre class=\"brush:xml;\">\r\nconst (\r\n   cityListReg = `&lt;a href=&quot;(http:\/\/www.zhenai.com\/zhenghun\/[0-9a-z]+)\"[^&gt;]*&gt;([^&lt;]+)&lt;\/a&gt;`\r\n )\r\n\r\n compile := regexp.MustCompile(cityListReg)\r\n\r\n submatch := compile.FindAllSubmatch(contents, -1)\r\n\r\n for _, m := range submatch {\r\n   fmt.Println(&quot;url:&quot; , string(m[1]), &quot;city:&quot;, string(m[2]))\r\n }<\/pre>\n<p>\n\t\u5339\u914d\u5305\u542bg g,\u4e14gg\u4e2d\u95f4\u81f3\u5c11\u4e00\u4e2a\u5c0f\u5199\u5b57\u6bcd\uff1a\n<\/p>\n<pre class=\"brush:groovy;\">\r\n\/\/\u5339\u914d\u5305\u542bg g,\u4e14gg\u4e2d\u95f4\u81f3\u5c11\u4e00\u4e2a\u5c0f\u5199\u5b57\u6bcd\r\n match, _ := regexp.MatchString(&quot;g([a-z]+)g&quot;, &quot;11golang11&quot;)\r\n \/\/true\r\n fmt.Println(match)<\/pre>\n<p>\n\t\u4e0a\u9762\u6211\u4eec\u76f4\u63a5\u4f7f\u7528\u4e86\u5b57\u7b26\u4e32\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4f46\u662f\u5bf9\u4e8e\u5176\u4ed6\u7684\u6b63\u5219\u5339\u914d\u4efb\u52a1\uff0c\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u4f18\u5316\u8fc7\u7684\u6b63\u5219\u5bf9\u8c61\uff1a\n<\/p>\n<pre class=\"brush:plain;\">\r\ncompile, err := regexp.Compile(&quot;smallsoup@gmail.com&quot;)\r\n\r\n if err != nil {\r\n   \/\/....\u6b63\u5219\u8bed\u6cd5\u9519\u8bef\uff0c\u9700\u8981\u5904\u7406\u9519\u8bef\r\n   fmt.Println(err)\r\n }\r\n\r\n \/\/smallsoup@gmail.com\r\n fmt.Println(compile.FindString(text))<\/pre>\n<p>\n\tcompile, err :=regexp.Compile(&quot;smallsoup@gmail.com&quot;)<br \/>\n\t\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5668\u548c\u9519\u8bef\uff0c\u5f53\u53c2\u6570\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0d\u7b26\u5408\u6b63\u5219\u8bed\u6cd5\u65f6\u8fd4\u56deerror\uff0c\u6bd4\u5982\u8bf4regexp.Compile(&quot;[smallsoup@gmail.com&#8221;)\u5c31\u4f1a\u62a5\u9519missing closing ]<br \/>\n\t\u4e00\u822c\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u7528\u6237\u8f93\u5165\u7684\u624d\u9700\u8981\u5904\u7406\u9519\u8bef\uff0c\u800c\u81ea\u5df1\u5199\u7684\u4e00\u822c\u662f\u4e0d\u4f1a\u6709\u9519\u7684\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528compile:= regexp.MustCompile(&quot;smallsoup@gmail.com&quot;)\uff0c\u5982\u679c\u8bed\u6cd5\u9519\u8bef\uff0c\u5c31\u4f1a\u53d1\u751fpanic\u3002\n<\/p>\n<pre class=\"brush:plain;\">\r\ntext1 := `my email is aa@qq.com\r\n  aa email is aa@gmail.com\r\n  bb email is bb@qq.com\r\n  cc email is cc@qq.com.cn\r\n  `\r\n \/\/\u5982\u679c\u8981\u63d0\u53d6A@B.C\u4e2d\u7684A\u3001B\u3001C\uff0c\u9700\u8981\u7528\u5230\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u63d0\u53d6\u529f\u80fd\u3002\r\n comp := regexp.MustCompile(`([a-zA-Z0-9]+)@([a-zA-Z0-9.]+)\\.([a-zA-Z0-9]+)`)\r\n\r\n \/\/\u5229\u7528\u81ea\u5339\u914d\u83b7\u53d6\u6b63\u5219\u8868\u8fbe\u5f0f\u91cc\u62ec\u53f7\u4e2d\u7684\u5339\u914d\u5185\u5bb9\r\n submatchs := comp.FindAllStringSubmatch(text1, -1)\r\n\r\n \/\/submatchs\u5176\u5b9e\u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\r\n fmt.Println(submatchs)\r\n\r\n \/\/\u53bb\u9664\u6bcf\u4e2a\u5339\u914d,submatch\u5176\u5b9e\u8fd8\u662f\u4e2aslice\r\n for _, submatch := range submatchs {\r\n   fmt.Println(submatch)\r\n }<\/pre>\n<p>\n\t\u7ed3\u679c\u8f93\u51fa\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:plain;\">\r\n[[aa@qq.com aa qq com] [aa@gmail.com aa gmail com] [bb@qq.com bb qq com] [cc@qq.com.cn cc qq.com cn]]\r\n[aa@qq.com aa qq com]\r\n[aa@gmail.com aa gmail com]\r\n[bb@qq.com bb qq com]\r\n[cc@qq.com.cn cc qq.com cn]<\/pre>\n<pre class=\"brush:plain;\">\r\nr := regexp.MustCompile(&quot;p([a-z]+)ch&quot;)\r\n fmt.Println(r) \/\/-----&gt;p([a-z]+)ch\r\n \/\/regexp \u5305\u4e5f\u53ef\u4ee5\u7528\u6765\u66ff\u6362\u90e8\u5206\u5b57\u7b26\u4e32\u4e3a\u5176\u4ed6\u503c\u3002\r\n fmt.Println(r.ReplaceAllString(&quot;a peach&quot;, &quot;&lt;smallsoup&gt;&quot;)) \/\/-----&gt;a &lt;smallsoup&gt;\r\n \/\/Func \u53d8\u91cf\u5141\u8bb8\u4f20\u9012\u5339\u914d\u5185\u5bb9\u5230\u4e00\u4e2a\u7ed9\u5b9a\u7684\u51fd\u6570\u4e2d\uff0c\r\n in := []byte(&quot;a smallsoup&quot;)\r\n out := r.ReplaceAllFunc(in, bytes.ToUpper)\r\n fmt.Println(string(out)) \/\/-----&gt;a PEACH\r\n \/*#######################\u5e38\u89c1\u8868\u8fbe\u5f0f###########################*\/\r\n \/\/ \u67e5\u627e\u6c49\u5b57\r\n testText := &quot;Hello \u4f60\u597d\u5417, I like golang!&quot;\r\n reg := regexp.MustCompile(`[\\p{Han}]+`)\r\n fmt.Println(reg.FindAllString(testText, -1)) \/\/ -----&gt;[\u4f60\u597d]\r\n reg = regexp.MustCompile(`[\\P{Han}]+`)\r\n fmt.Println(reg.FindAllString(testText, -1))        \/\/ -----&gt;[\"Hello \" \", I li golang!\"]\r\n fmt.Printf(&quot;%q\\n&quot;, reg.FindAllString(testText, -1)) \/\/ -----&gt;[\"Hello \" \", I lm golang!\"]\r\n \/\/Email\r\n reg = regexp.MustCompile(`\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*`)\r\n fmt.Println(reg.MatchString(&quot;smallsoup@qq.com&quot;))\r\n \/\/\u7528\u6237\u540d\u5bc6\u7801\uff1a\r\n reg = regexp.MustCompile(`[a-zA-Z]|\\w{6,18}`)\r\n fmt.Println(reg.MatchString(&quot;w_dy_246&quot;))<\/pre>\n<p>\n\t\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:plain;\">\r\np([a-z]+)ch\r\na &lt;smallsoup&gt;\r\na smallsoup\r\n[\u4f60\u597d\u5417]\r\n[Hello  , I like golang!]\r\n[\"Hello \" \", I like golang!\"]\r\ntrue\r\ntrue\r\n\r\nProcess finished with exit code 0<\/pre>\n<p>\n\t\u4ee5\u4e0a\u5c31\u662f go \u8bed\u8a00\u7684\u76f8\u5173\u6b63\u5219\u8868\u8fbe\u5f0f\u6559\u7a0b\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u4e0b\u65b9\u7684\u5fae\u4fe1\u516c\u4f17\u53f7\uff0c\u5171\u540c\u5b66\u4e60\uff0c\u5171\u540c\u8fdb\u6b65\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=2896\">go\u8bed\u8a00\u6b63\u5219\u8868\u8fbe\u5f0f\u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u4e2a\u7ecf\u5e38\u4f1a\u4f7f\u7528\u7684\u529f\u80fd\uff0c\u5c24\u5176\u662f\u722c\u866b\u5e94\u7528\u4e2d\u3002java \u4e2d\u6709\u5bf9\u5e94\u7684\u9009\u62e9\u5668\uff0cgo \u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u81ea\u5df1\u5c01 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[118,6],"tags":[1654,1048,1038,108,1031],"class_list":["post-2896","post","type-post","status-publish","format-standard","hentry","category-ndisdevelop","category-yyzt","tag-go","tag-webmagic","tag-1038","tag-108","tag-1031"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2896","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2896"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2896\/revisions"}],"predecessor-version":[{"id":2897,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2896\/revisions\/2897"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}