{"id":239576,"date":"2022-04-18T08:15:24","date_gmt":"2022-04-18T00:15:24","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=239576"},"modified":"2022-04-02T12:09:31","modified_gmt":"2022-04-02T04:09:31","slug":"perl-split-string","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/perl-split-string.html","title":{"rendered":"Perl split\u5b57\u7b26\u4e32\u5206\u5272\u51fd\u6570\u7528\u6cd5\u6307\u5357"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u672c\u6587\u5411\u5927\u5bb6\u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0bPerl split\u51fd\u6570\u7684\u7528\u6cd5\uff0cPerl\u4e2d\u7684\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u51fd\u6570\u662fPerl split\u51fd\u6570-\u628a\u5b57\u7b26\u4e32\u8fdb\u884c\u5206\u5272\u5e76\u628a\u5206\u5272\u540e\u7684\u7ed3\u679c\u653e\u5165\u6570\u7ec4\u4e2d\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u672c\u6587\u548c\u5927\u5bb6\u91cd\u70b9\u8ba8\u8bba\u4e00\u4e0bPerl split\u51fd\u6570\u7684\u7528\u6cd5\uff0cPerl\u4e2d\u7684\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u51fd\u6570\u662fPerl split\u51fd\u6570-\u628a\u5b57\u7b26\u4e32\u8fdb\u884c\u5206\u5272\u5e76\u628a\u5206\u5272\u540e\u7684\u7ed3\u679c\u653e\u5165\u6570\u7ec4\u4e2d\u3002\u8fd9\u4e2aPerl split\u51fd\u6570\u4f7f\u7528\u89c4\u5219\u8868\u8fbe\u5f0f\uff08RE\uff09\uff0c\u5982\u679c\u672a\u7279\u5b9a\u5219\u5de5\u4f5c\u5728$_\u53d8\u91cf\u4e0a\u3002<\/p>\n

Perl split\u51fd\u6570<\/strong><\/div>\n

Perl\u4e2d\u7684\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u51fd\u6570\u662fPerl split\u51fd\u6570-\u628a\u5b57\u7b26\u4e32\u8fdb\u884c\u5206\u5272\u5e76\u628a\u5206\u5272\u540e\u7684\u7ed3\u679c\u653e\u5165\u6570\u7ec4\u4e2d\u3002\u8fd9\u4e2aPerl split\u51fd\u6570\u4f7f\u7528\u89c4\u5219\u8868\u8fbe\u5f0f\uff08RE\uff09\uff0c\u5982\u679c\u672a\u7279\u5b9a\u5219\u5de5\u4f5c\u5728$_\u53d8\u91cf\u4e0a\u3002<\/p>\n

Perl split\u51fd\u6570\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\uff1a <\/p>\n

\r\n$info=\"Caine:Michael:Actor:14,LeafyDrive\"; \r\n@personal=split(\/:\/,$info); \r\n<\/pre>\n

\u5176\u7ed3\u679c\u662f\uff1a@personal=(\"Caine\",\"Michael\",\"Actor\",\"14,LeafyDrive\");<\/code>
\n\u25c6\u5982\u679c\u6211\u4eec\u5df2\u7ecf\u628a\u4fe1\u606f\u5b58\u653e\u5728$_\u53d8\u91cf\u4e2d\uff0c\u90a3\u4e48\u53ef\u4ee5\u8fd9\u6837\uff1a<\/p>\n

\r\n@personal=split(\/:\/);\r\n<\/pre>\n

\u5982\u679c\u5404\u4e2a\u57df\u88ab\u4efb\u4f55\u6570\u91cf\u7684\u5192\u53f7\u5206\u9694\uff0c\u53ef\u4ee5\u7528RE\u4ee3\u7801\u8fdb\u884c\u5206\u5272\uff1a <\/p>\n

\r\n$_=\"Capes:Geoff::Shotputter:::BigAvenue\"; \r\n@personal=split(\/:+\/); \r\n<\/pre>\n

\u5176\u7ed3\u679c\u662f\uff1a@personal=(\"Capes\",\"Geoff\",\"Shotputter\",\"BigAvenue\");<\/p>\n

\u4f46\u662f\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n

\r\n$_=\"Capes:Geoff::Shotputter:::BigAvenue\"; \r\n@personal=split(\/:\/);\r\n<\/pre>\n

\u7684\u7ed3\u679c\u662f\uff1a@personal=(\"Capes\",\"Geoff\",\"\",\"Shotputter\",\"\",\"\",\"BigAvenue\");<\/code><\/p>\n

\u8fd9\u4e2aPerl split\u51fd\u6570\u4e2d\u5355\u8bcd\u53ef\u4ee5\u88ab\u5206\u5272\u6210\u5b57\u7b26\uff0c\u53e5\u5b50\u53ef\u4ee5\u88ab\u5206\u5272\u6210\u5355\u8bcd\uff0c\u6bb5\u843d\u53ef\u4ee5\u88ab\u5206\u5272\u6210\u53e5\u5b50\uff1a<\/p>\n

\r\n@chars=split(\/\/,$word);\r\n@words=split(\/\/,$sentence); \r\n@sentences=split(\/\\.\/,$paragraph); \r\n<\/pre>\n

\u5728\u7b2c\u4e00\u53e5\u4e2d\uff0c\u7a7a\u5b57\u7b26\u4e32\u5728\u6bcf\u4e2a\u5b57\u7b26\u95f4\u5339\u914d\uff0c\u6240\u4ee5@chars\u6570\u7ec4\u662f\u4e00\u4e2a\u5b57\u7b26\u7684\u6570\u7ec4\u3002>><\/p>\n

\/\/\u4e4b\u95f4\u7684\u90e8\u5206\u8868\u793asplit\u7528\u5230\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff08\u6216\u8005\u8bf4\u5206\u9694\u6cd5\u5219\uff09
\n\\s\u662f\u4e00\u79cd\u901a\u914d\u7b26\uff0c\u4ee3\u8868\u7a7a\u683c
\n+\u4ee3\u8868\u91cd\u590d\u4e00\u6b21\u6216\u8005\u4e00\u6b21\u4ee5\u4e0a\u3002
\n\u6240\u4ee5\uff0c\\s+\u4ee3\u8868\u4e00\u4e2a\u6216\u8005\u4e00\u4e2a\u4ee5\u4e0a\u7684\u7a7a\u683c\u3002
\nsplit(\/\\s+\/,$line)\u8868\u793a\u628a\u5b57\u7b26\u4e32$line,\u6309\u7a7a\u683c\u4e3a\u754c\u5206\u5f00\u3002
\n\u6bd4\u5982\u8bf4\uff0c$line=\"\u4f60\u597d\u670b\u53cb\u6b22\u8fce\u5149\u4e34\u6211\u7684\u7f51\u7ad9jb51.net\"\uff1b
\nsplit(\/\\s+\/,$line)\u540e\u5f97\u5230\uff1a
\n\u4f60\u597d\u670b\u53cb\u6b22\u8fce\u8bbf\u95ee\u6211\u7684\u7f51\u7ad9jb51.net<\/p>\n

\u4e00\u822c\u7528\u6cd5\uff1a @somearray = split(\/:+\/, $string ); #\u62ec\u53f7\u53ef\u4ee5\u4e0d\u8981\u3002 \u82e5\u4e0d\u6307\u5b9a$string, \u5219\u5bf9\u9ed8\u8ba4\u53d8\u91cf$_\u64cd\u4f5c\uff0c \u4e24\u659c\u7ebf\u95f4\u4e3a\u5206\u5272\u7b26\uff0c\u53ef\u4ee5\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5f3a\u608d\u5f02\u5e38\u3002<\/p>\n

\u5728perl\u624b\u518c\u91cc\uff0c\u6709\u4e00\u4e2a\u7528\u6cd5\u4e0d\u591a\u89c1\u3002\u5373\uff1a split \/PATTERN\/, EXPR, LIMIT\uff1b \u5173\u952e\u5c31\u662f\u8fd9\u4e2aLIMIT\u53c2\u6570\uff0c\u53ef\u4ee5\u8282\u7701\u4e0d\u5c11\u4e8b\u60c5\u3002 \u5982\u679c\u4f7f\u7528\u4e86LIMIT\uff0c\u4e14\u662f\u6b63\u6570\uff0c\u8868\u793a\u5206\u5272\u6210\u4e0d\u591a\u4e8eLIMIT\u6307\u5b9a\u7684\u6570\u76ee\u7684\u57df\u3002<\/p>\n

\u901a\u8fc7\u5236\u5b9aLIMIT\uff0c\u53ef\u4ee5\u5728\u5f88\u957f(\u5206\u5272\u4ea7\u751f\u51e0\u4e07\u4e2a\u5143\u7d20or\u57df)\u7684\u884c\u5206\u5272\u64cd\u4f5c\u4e2d\uff0c\u53ea\u8fd4\u56de\u5173\u952e\u7684\u524d\u51e0\u5217\u7684\u57df\u503c\uff0c\u51cf\u5c11\u4e86\u5185\u5b58\u4f7f\u7528\u53ca\u65f6\u95f4\u6d88\u8017\u3002\u6bd4\u5982\u4e00\u822c\u7684\u57fa\u56e0\u578b\u6570\u636e\uff0c\u7b2c\u4e00\u5217\u901a\u5e38\u662f\u6750\u6599\u547d\u540d\uff0c\u9700\u8981\u901a\u8fc7\u6750\u6599\u540d\u7684\u5224\u65ad\u53d6\u820d\uff0c\u8fd9\u65f6\u5019\u5c31\u53ef\u4ee5\u8fd9\u6837\u7528\u3002 my ($firstfield) = split \/\\t\/, $someline, 1; \u5982\u679c\u9700\u8981\u524d\u9762\u51e0\u5217\u7684\u503c\uff0c\u8fd9\u6837\u7684\u65b9\u5f0f\u5bf9\u5927\u6587\u4ef6\u6548\u7387\u5f88\u597d\uff1a my \uff08undef, $var1, undef, undef, undef, $var2\uff09=split \/\\t\/, $someline, 6;<\/p>\n

\u6709\u7f51\u53cb\u5bf9\u8fd9\u79cd\u65b9\u5f0f\u505a\u4e86\u6d4b\u8bd5\uff0c\u663e\u793a\u8f83\u597d\u3002\u5f15\u7528\u5982\u4e0b\uff1a
\n\u4e00\u4e2a\u6587\u4ef6\uff0c\u6bcf\u884c\u90fd\u670918\u9879\uff0c\u5404\u9879\u4e4b\u95f4\u7528\\t\u5206\u5272\uff0c\u4f7f\u7528\u65f6\u7528\u5230\u4e86\u7b2c6\u9879\uff0c\u6298\u817e\u4e86\u51e0\u79cd\u7528\u6cd5<\/p>\n

\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:<\/p>\n

\r\nmy @array = split(\"\\t\",$_); my $var = $array[6];   \u6d4b\u8bd5\u6587\u4ef6\u5e73\u5747\u7528\u65f68.2s\r\n my($var) = (split(\"\\t\",$_))[6];  \u6d4b\u8bd5\u5e73\u5747\u7528\u65f65.1s\r\n my(undef,undef,undef,undef,undef,undef,$var) = split(\"\\t\",$_); \u5e73\u5747\u7528\u65f63.53s\r\n my(undef,undef,undef,undef,undef,undef,$var) = split(\"\\t\",$_,7);\u5e73\u5747\u7528\u65f63.52s\r\n my $var = (split(\"\\t\",$_,7))[6]; \u5e73\u5747\u7528\u65f63.53s\r\n<\/pre>\n
\r\n\u770b\u6765\u540e3\u79cd\u624d\u662f\u738b\u9053\uff0c\u5982\u679c\u9700\u8981\u4f7f\u7528\u591a\u9879\u4e5f\u53ef\u4ee5\u8fdb\u884c\u8fdb\u884c\u9002\u5f53\u7684\u53d8\u52a8\u3002\u4e0d\u8fc7\u4e24\u9879\u5982\u679c\u8de8\u5ea6\u6bd4\u8f83\u5927\uff0c3\uff0c4\u5e94\u8be5\u662f\u4e0d\u9519\u9009\u62e9\uff0c5\u5c31\u53ea\u80fd\u7528\u4e2d\u95f4\u6570\u7ec4\u3002\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u672c\u6587\u548c\u5927\u5bb6\u91cd\u70b9\u8ba8\u8bba\u4e00\u4e0bPerl split\u51fd\u6570\u7684\u7528\u6cd5\uff0cPerl\u4e2d\u7684\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u51fd\u6570\u662fPerl split\u51fd\u6570 […]<\/p>\n","protected":false},"author":1903,"featured_media":239578,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-239576","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=239576"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239576\/revisions"}],"predecessor-version":[{"id":239577,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239576\/revisions\/239577"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/239578"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=239576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=239576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=239576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}