{"id":118,"date":"2025-05-31T10:50:37","date_gmt":"2025-06-05T03:14:37","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=118"},"modified":"2025-06-05T10:50:37","modified_gmt":"2025-06-05T03:14:37","slug":"swift-%e6%ad%a3%e8%a6%8f%e8%a1%a8%e9%81%94%e5%bc%8f-%e9%9b%bb%e8%a9%b1-%e8%ba%ab%e5%88%86%e8%ad%89-email","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-%e6%ad%a3%e8%a6%8f%e8%a1%a8%e9%81%94%e5%bc%8f-%e9%9b%bb%e8%a9%b1-%e8%ba%ab%e5%88%86%e8%ad%89-email\/","title":{"rendered":"\u6700\u65b0 Swift \u6b63\u898f\u8868\u9054\u5f0f\u6559\u5b78\uff1a\u96fb\u8a71\u3001\u8eab\u5206\u8b49\u548c\u96fb\u5b50\u90f5\u4ef6\u7684\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<h2>Swift 5.0 \u6b63\u898f\u8868\u9054\u5f0f\u57fa\u672c\u8a9e\u6cd5\u7bc4\u4f8b<\/h2>\n<p>\u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e Swift 5.0 \u7684\u6b63\u898f\u8868\u9054\u5f0f\u4f7f\u7528\u65b9\u6cd5\uff0c\u7279\u5225\u662f\u5982\u4f55\u7528\u5b83\u4f86\u9a57\u8b49\u96fb\u8a71\u865f\u78bc\u3001\u8eab\u5206\u8b49\u865f\u78bc\u548c\u96fb\u5b50\u90f5\u4ef6\u5730\u5740\u3002\u9019\u4e9b\u793a\u4f8b\u5c07\u5c55\u793a\u5728 2025 \u5e74\u6700\u65b0\u7684 Swift \u8a9e\u6cd5\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h3>\u57fa\u672c\u8a9e\u6cd5<\/h3>\n<p>\u9996\u5148\uff0c\u8b93\u6211\u5011\u770b\u4e00\u500b\u57fa\u672c\u7684\u4f8b\u5b50\u4f86\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u6b63\u898f\u8868\u9054\u5f0f\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">let yourStr = \"hello\"\nlet result = yourStr.range(of: \"he\", options: .regularExpression) != nil\n<\/code><\/pre>\n<h4>\u7d50\u679c<\/h4>\n<pre><code class=\"language-swift line-numbers\">true\n<\/code><\/pre>\n<h3>\u8655\u7406\u4e2d\u6587\u5b57\u7b26<\/h3>\n<p>Swift \u7684\u6b63\u898f\u8868\u9054\u5f0f\u4e5f\u80fd\u8655\u7406\u4e2d\u6587\u5b57\u7b26\uff0c\u4ee5\u4e0b\u662f\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let yourStr = \"\u53f2\u8482\u82ac\u8cf4\"\nlet result = yourStr.range(of: \"\u8cf4\", options: .regularExpression) != nil\n<\/code><\/pre>\n<h3>\u4f7f\u7528\u6b63\u898f\u8868\u9054\u5f0f\u7b26\u865f<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u6b63\u898f\u8868\u9054\u5f0f\u7b26\u865f\u53ca\u5176\u7528\u6cd5\uff1a<\/p>\n<h4>[ . ] \u4efb\u610f\u5b57\u4e32\u7b26\u865f\uff08\u4e0d\u5305\u542b \\n\uff09<\/h4>\n<pre><code class=\"language-swift line-numbers\">let yourStr = \"\u53f2\u8482\u82ac\u8cf4+1232kdsjsaoj\"\nlet result = yourStr.range(of: \".\", options: .regularExpression) != nil\n<\/code><\/pre>\n<h4>[ \\d ] \u5305\u542b\u6578\u5b57<\/h4>\n<p>\u9019\u500b\u7bc4\u4f8b\u6aa2\u67e5\u662f\u5426\u5305\u542b\u6578\u5b57\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let yourStr = \"aaaaaaaaa1\"\nlet result = yourStr.range(of: \"\\\\d\", options: .regularExpression) != nil\n<\/code><\/pre>\n<h4>[ \\D ] \u4e0d\u5305\u542b\u6578\u5b57<\/h4>\n<p>\u6b64\u7bc4\u4f8b\u78ba\u8a8d\u5b57\u4e32\u662f\u5426\u5b8c\u5168\u4e0d\u5305\u542b\u6578\u5b57\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let yourStr = \"45465464\"\nlet result = yourStr.range(of: \"\\\\D\", options: .regularExpression) != nil\n<\/code><\/pre>\n<h4>\u7d50\u679c<\/h4>\n<pre><code class=\"language-swift line-numbers\">false\n<\/code><\/pre>\n<h4>\u6df7\u642d\u5b57\u4e32<\/h4>\n<p>\u6aa2\u67e5\u5305\u542b\u6578\u5b57\u548c\u5b57\u6bcd\u7684\u5b57\u4e32\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let yourStr = \"test123\"\nlet result = yourStr.range(of: \"\\\\D\", options: .regularExpression) != nil\n<\/code><\/pre>\n<h4>\u7d50\u679c<\/h4>\n<pre><code class=\"language-swift line-numbers\">true\n<\/code><\/pre>\n<h4>[ \\s ] \u5305\u542b\u7a7a\u767d<\/h4>\n<p>\u6aa2\u67e5\u5b57\u4e32\u4e2d\u662f\u5426\u6709\u7a7a\u767d\u5b57\u7b26\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let yourStr = \"123 test\"\nlet result = yourStr.range(of: \"\\\\s\", options: .regularExpression) != nil\n<\/code><\/pre>\n<h4>[ \\w ] \u5305\u542b\u6578\u5b57\u3001\u5927\u5c0f\u5beb\u5b57\u6bcd\u548c\u5e95\u7dda<\/h4>\n<pre><code class=\"language-swift line-numbers\">let yourStr = \"test_Test\/\"\nlet result = yourStr.range(of: \"\\\\w\", options: .regularExpression) != nil\n<\/code><\/pre>\n<h4>[ \\W ] \u4e0d\u5305\u542b\u6578\u5b57\u3001\u5927\u5c0f\u5beb\u5b57\u6bcd\u548c\u5e95\u7dda<\/h4>\n<p>\u78ba\u8a8d\u5b57\u4e32\u4e2d\u4e0d\u5305\u542b\u6578\u5b57\u3001\u5927\u5c0f\u5beb\u5b57\u6bcd\u548c\u5e95\u7dda\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let yourStr = \"test_Test\/\"\nlet result = yourStr.range(of: \"\\\\W\", options: .regularExpression) != nil\n<\/code><\/pre>\n<h4><input style=\"margin-right:5px\" type=\"checkbox\" class=\"task-list-item-checkbox\" disabled \/>\u7b26\u5408\u5176\u4e2d\u4e00\u9805<\/h4>\n<p>\u6aa2\u67e5\u5b57\u4e32\u4e2d\u662f\u5426\u5305\u542b\u4efb\u4e00\u6307\u5b9a\u5b57\u7b26\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let yourStr = \"123456a\"\nlet result = yourStr.range(of: \"[abc]\", options: .regularExpression) != nil\n<\/code><\/pre>\n<h4>[ + ] \u641c\u5c0b\u91cd\u8907\u5b57\u4e32<\/h4>\n<p>\u6aa2\u67e5\u5b57\u4e32\u4e2d\u662f\u5426\u6709\u91cd\u8907\u7684\u5b57\u6bcd\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let yourStr = \"adad adadadad\"\nlet result = yourStr.range(of: \"(ad)+\", options: .regularExpression) != nil\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528\u6b63\u898f\u8868\u9054\u5f0f\u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<br \/>\n&#8211; \u8a9e\u6cd5\u932f\u8aa4\uff1a\u78ba\u4fdd\u6b63\u898f\u8868\u9054\u5f0f\u7684\u7b26\u865f\u4f7f\u7528\u6b63\u78ba\u3002<br \/>\n&#8211; \u5ffd\u7565\u5b57\u5143\u8f49\u7fa9\uff1a\u9700\u8981\u8f49\u7fa9\u7684\u5b57\u7b26\u5982 `\\` \u5fc5\u9808\u6b63\u78ba\u8655\u7406\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u4f7f\u7528\u6b63\u898f\u8868\u9054\u5f0f\u53ef\u4ee5\u5728\u8a31\u591a\u9818\u57df\u4e2d\u767c\u63ee\u4f5c\u7528\uff0c\u4f8b\u5982\uff1a<br \/>\n&#8211; \u9a57\u8b49\u7528\u6236\u8f38\u5165\u7684\u96fb\u5b50\u90f5\u4ef6\u5730\u5740\u3002<br \/>\n&#8211; \u6aa2\u67e5\u96fb\u8a71\u865f\u78bc\u7684\u683c\u5f0f\u3002<br \/>\n&#8211; \u89e3\u6790\u548c\u8655\u7406\u5404\u7a2e\u6587\u672c\u6578\u64da\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Swift \u7684\u6b63\u898f\u8868\u9054\u5f0f\u529f\u80fd\u5f37\u5927\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5728\u8655\u7406\u5b57\u7b26\u4e32\u6642\u9054\u5230\u66f4\u9ad8\u7684\u9748\u6d3b\u6027\u8207\u6548\u7387\u3002\u5e0c\u671b\u672c\u7bc7\u6559\u5b78\u80fd\u5920\u5e6b\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528 Swift \u7684\u6b63\u898f\u8868\u9054\u5f0f\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. Swift \u4e2d\u5982\u4f55\u4f7f\u7528\u6b63\u898f\u8868\u9054\u5f0f\u9a57\u8b49\u96fb\u5b50\u90f5\u4ef6\u683c\u5f0f\uff1f<\/h3>\n<p>\u4f7f\u7528\u6b63\u898f\u8868\u9054\u5f0f\u53ef\u4ee5\u6aa2\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u7b26\u5408\u96fb\u5b50\u90f5\u4ef6\u7684\u683c\u5f0f\uff0c\u793a\u4f8b\u53ef\u53c3\u8003\u4ee5\u4e0b\u4ee3\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let emailStr = \"example@test.com\"\nlet emailPattern = \"^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\\\.[A-Z|a-z]{2,}$\"\nlet result = emailStr.range(of: emailPattern, options: .regularExpression) != nil\n<\/code><\/pre>\n<h3>2. \u5982\u4f55\u6aa2\u67e5\u96fb\u8a71\u865f\u78bc\u662f\u5426\u6709\u6548\uff1f<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528\u6b63\u898f\u8868\u9054\u5f0f\u6aa2\u67e5\u96fb\u8a71\u865f\u78bc\u662f\u5426\u7b26\u5408\u7279\u5b9a\u683c\u5f0f\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let phoneStr = \"0912345678\"\nlet phonePattern = \"^09[0-9]{8}$\"\nlet result = phoneStr.range(of: phonePattern, options: .regularExpression) != nil\n<\/code><\/pre>\n<h3>3. Swift \u4e2d\u5982\u4f55\u6709\u6548\u5730\u8655\u7406\u591a\u884c\u5b57\u7b26\u4e32\u7684\u6b63\u898f\u8868\u9054\u5f0f\uff1f<\/h3>\n<p>\u5728\u8655\u7406\u591a\u884c\u5b57\u7b26\u4e32\u6642\uff0c\u53ef\u4ee5\u4f7f\u7528 `options: .anchorsMatchLines` \u4f86\u5339\u914d\u884c\u9996\u548c\u884c\u5c3e\uff0c\u9019\u6a23\u53ef\u4ee5\u66f4\u9748\u6d3b\u5730\u8655\u7406\u6b63\u898f\u8868\u9054\u5f0f\u3002<\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/p.ecpay.com.tw\/99527\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png\" alt=\"Swift \u8ab2\u7a0b\u5716\u7247\u8aaa\u660e\uff1a\u5b78\u7fd2 Swift \u6b63\u898f\u8868\u9054\u5f0f\u7684\u6700\u4f73\u8cc7\u6e90\" title=\"\"><\/a><br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>let yourStr = &#8220;hello&#8221;<br \/>\nlet result = yourStr.range(of: &#8220;he&#8221;, options: .regularExpression) != nil<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-118","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1U","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=118"}],"version-history":[{"count":7,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/118\/revisions"}],"predecessor-version":[{"id":1072,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/118\/revisions\/1072"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}