{"id":719,"date":"2023-06-01T12:04:55","date_gmt":"2023-06-01T04:04:55","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/?p=719"},"modified":"2023-06-01T12:04:55","modified_gmt":"2023-06-01T04:04:55","slug":"ios%e5%bc%80%e5%8f%91%e6%ad%a3%e5%88%99%e5%8c%b9%e9%85%8d%e5%8f%af%e4%bb%a5%e5%ae%9e%e7%8e%b0%e5%98%9b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2023\/719\/","title":{"rendered":"ios\u5f00\u53d1\u6b63\u5219\u5339\u914d\u53ef\u4ee5\u5b9e\u73b0\u561b\uff1f"},"content":{"rendered":"

\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6587\u672c\u5339\u914d\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u5b57\u7b26\u4e32\u5904\u7406\u3002\u5728 iOS \u5f00\u53d1\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u4e5f\u662f\u4e00\u79cd\u975e\u5e38\u5e38\u7528\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u3001\u89e3\u6790\u6587\u672c\u7b49\u573a\u666f\u3002<\/p>\n

\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5<\/p>\n

\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u7531\u4e00\u4e9b\u7279\u5b9a\u5b57\u7b26\u548c\u666e\u901a\u5b57\u7b26\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u7528\u6765\u63cf\u8ff0\u5b57\u7b26\u4e32\u7684\u6a21\u5f0f\u3002\u5728 iOS \u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 NSRegularExpression \u7c7b\u6765\u64cd\u4f5c\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n

\u4e0b\u9762\u662f\u4e00\u4e9b\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5\uff1a<\/p>\n

1. \u666e\u901a\u5b57\u7b26\uff1a\u8868\u793a\u4e0e\u8be5\u5b57\u7b26\u5b8c\u5168\u5339\u914d\u3002<\/p>\n

2. \u7279\u6b8a\u5b57\u7b26\uff1a\u8868\u793a\u4e0e\u8be5\u5b57\u7b26\u6240\u4ee3\u8868\u7684\u542b\u4e49\u5339\u914d\u3002<\/p>\n

3. \u5b57\u7b26\u96c6\u5408\uff1a\u8868\u793a\u5339\u914d\u5176\u4e2d\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\u3002<\/p>\n

4. \u8303\u56f4\uff1a\u8868\u793a\u5339\u914d\u5176\u4e2d\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\uff0c\u4e14\u8be5\u5b57\u7b26\u5fc5\u987b\u5728\u6307\u5b9a\u7684\u8303\u56f4\u5185\u3002<\/p>\n

5. \u91cf\u8bcd\uff1a\u8868\u793a\u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n

6. \u5206\u7ec4\uff1a\u8868\u793a\u5c06\u4e00\u7ec4\u5b57\u7b26\u770b\u4f5c\u6574\u4f53\u8fdb\u884c\u5339\u914d\u3002<\/p>\n

7. \u8f6c\u4e49\u5b57\u7b26\uff1a\u8868\u793a\u8f6c\u4e49\u7279\u6b8a\u5b57\u7b26\u3002<\/p>\n

8. \u951a\u70b9\uff1a\u8868\u793a\u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u5934\u6216\u7ed3\u5c3e\u3002<\/p>\n

\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4f7f\u7528<\/p>\n

\u5728 iOS \u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 NSRegularExpression \u7c7b\u6765\u521b\u5efa\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528\u8be5\u5bf9\u8c61\u6765\u5339\u914d\u5b57\u7b26\u4e32\u3002<\/p>\n

\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n

“`<\/p>\n

NSString *pattern = @”[0-9]+”;<\/p>\n

NSString *string = @”12345″;<\/p>\n

NSError *error = nil;<\/p>\n

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];<\/p>\n

NSRange range = NSMakeRange(0, string.length);<\/p>\n

BOOL isMatch = [regex firstMatchInString:string options:0 range:range] != nil;<\/p>\n

NSLog(@”isMatch: %d”, isMatch);<\/p>\n

“`<\/p>\n

\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f `[0-9]+` \u6765\u5339\u914d\u5b57\u7b26\u4e32 `12345`\u3002\u8be5\u6b63\u5219\u8868\u8fbe\u5f0f\u8868\u793a\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\u3002\u6211\u4eec\u4f7f\u7528 NSRegularExpression \u7c7b\u7684 `regularExpressionWithPattern:options:error:` \u65b9\u6cd5\u6765\u521b\u5efa\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528\u8be5\u5bf9\u8c61\u7684 `firstMatchInString:options:range:` \u65b9\u6cd5\u6765\u5339\u914d\u5b57\u7b26\u4e32\u3002\u5982\u679c\u5339\u914d\u6210\u529f\uff0c\u5219\u8fd4\u56de\u4e00\u4e2a NSTextCheckingResult \u5bf9\u8c61\uff0c\u5426\u5219\u8fd4\u56de nil\u3002<\/p>\n

\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u9ad8\u7ea7\u7528\u6cd5<\/p>\n

\u9664\u4e86\u57fa\u672c\u8bed\u6cd5\u4e4b\u5916\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u8fd8\u6709\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5339\u914d\u3002<\/p>\n

1. \u975e\u8d2a\u5a6a\u5339\u914d<\/p>\n

\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u8d2a\u5a6a\u7684\uff0c\u5373\u5c3d\u53ef\u80fd\u5339\u914d\u66f4\u591a\u7684\u5b57\u7b26\u3002\u5982\u679c\u60f3\u8981\u5339\u914d\u5c3d\u53ef\u80fd\u5c11\u7684\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u975e\u8d2a\u5a6a\u5339\u914d\u3002\u975e\u8d2a\u5a6a\u5339\u914d\u4f7f\u7528 `?` \u7b26\u53f7\u6765\u8868\u793a\u3002<\/p>\n

\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n

“`<\/p>\n

NSString *pattern = @”(.*?)\\\\d+”;<\/p>\n

NSString *string = @”abc123def456″;<\/p>\n

NSError *error = nil;<\/p>\n

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];<\/p>\n

NSRange range = NSMakeRange(0, string.length);<\/p>\n

NSTextCheckingResult *result = [regex firstMatchInString:string options:0 range:range];<\/p>\n

NSString *matchedString = [string substringWithRange:[result rangeAtIndex:1]];<\/p>\n

NSLog(@”matchedString: %@”, matchedString);<\/p>\n

“`<\/p>\n

\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f `(.*?)\\\\d+` \u6765\u5339\u914d\u5b57\u7b26\u4e32 `abc123def456`\u3002\u8be5\u6b63\u5219\u8868\u8fbe\u5f0f\u8868\u793a\u5339\u914d\u4efb\u610f\u5b57\u7b26\uff0c\u76f4\u5230\u9047\u5230\u6570\u5b57\u4e3a\u6b62\u3002\u6211\u4eec\u4f7f\u7528 `?` \u7b26\u53f7\u6765\u5c06\u5339\u914d\u6a21\u5f0f\u8bbe\u7f6e\u4e3a\u975e\u8d2a\u5a6a\u6a21\u5f0f\u3002\u5339\u914d\u7ed3\u679c\u4e3a\u5b57\u7b26\u4e32 `abc`\u3002<\/p>\n

2. \u5411\u524d\u67e5\u627e<\/p>\n

<\/figure>\n<\/p>\n

\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5411\u524d\u67e5\u627e\u53ef\u4ee5\u7528\u6765\u5339\u914d\u67d0\u4e2a\u4f4d\u7f6e\u4e4b\u524d\u7684\u5b57\u7b26\u4e32\u3002\u5411\u524d\u67e5\u627e\u4f7f\u7528 `(?=)` \u7b26\u53f7\u6765\u8868\u793a\u3002<\/p>\n

\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n

“`<\/p>\n

NSString *pattern = @”\\\\d+(?=\u5143)”;<\/p>\n

NSString *string = @”100\u5143”;<\/p>\n

NSError *error = nil;<\/p>\n

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];<\/p>\n

NSRange range = NSMakeRange(0, string.length);<\/p>\n

NSTextCheckingResult *result = [regex firstMatchInString:string options:0 range:range];<\/p>\n

NSString *matchedString = [string substringWithRange:result.rangewordpress ios<\/a>];<\/p>\n

NSLog(@”matchedString: %@”, matchedString);<\/p>\n

“`<\/p>\n

\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f `\\\\d+(?=\u5143)` \u6765\u5339\u914d\u5b57\u7b26\u4e32 `100\u5143`\u3002\u8be5\u6b63\u5219\u8868\u8fbe\u5f0f\u8868\u793a\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\uff0c\u4f46\u8be5\u6570\u5b57\u540e\u9762\u5fc5\u987b\u7d27\u8ddf\u7740\u5b57\u7b26\u4e32 `\u5143`\u3002\u6211\u4eec\u4f7f\u7528 `(?=)` \u7b26\u53f7\u6765\u8fdb\u884c\u5411\u524d\u67e5\u627e\u3002\u5339\u914d\u7ed3\u679c\u4e3a\u5b57\u7b26\u4e32 `100`\u3002<\/p>\n

3. \u5411\u540e\u67e5\u627e<\/p>\n

\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5411\u540e\u67e5\u627e\u53ef\u4ee5\u7528\u6765\u5339\u914d\u67d0\u4e2a\u4f4d\u7f6e\u4e4b\u540e\u7684\u5b57\u7b26\u4e32\u3002\u5411\u540ehtml\u5c01\u88c5exe<\/a>\u67e5\u627e\u4f7f\u7528 `(?<\/p>\n

\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n

“`<\/p>\n

NSString *pattern = @”(?<\/p>\n

NSString *string = @”1234567890″;<\/p>\n

NSError *error = nil;<\/p>\n

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];<\/p>\n

NSRange range = NSMakeRange(0, string.length);<\/p>\n

NSTextCheckingResult *result = [regex firstMatchInString:string options:0 range:range];<\/p>\n

NSString *matchedString = [string substringWithRange:result.range];<\/p>\n

NSLog(@”matchedString: %@”, matchedString);<\/p>\n

“`<\/p>\n

\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f `(?<\/p>\n

\u603b\u7ed3<\/p>\n

\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u6587\u672c\u5339\u914d\u5de5\u5177\uff0c\u5728 iOS \u5f00\u53d1\u4e2d\u975e\u5e38\u5e38\u7528\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5\u548c\u9ad8\u7ea7\u7528\u6cd5\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6587\u672c\u5339\u914d\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u5b57\u7b26\u4e32\u5904\u7406\u3002\u5728 iOS \u5f00\u53d1\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u4e5f\u662f\u4e00\u79cd\u975e\u5e38\u5e38\u7528\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u3001\u89e3\u6790\u6587\u672c\u7b49\u573a\u666f\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u7531\u4e00\u4e9b<\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[1675,1673,1676,1672,1674],"class_list":["post-719","post","type-post","status-publish","format-standard","hentry","category-zuoapp","tag-android-h5","tag-android-","tag-1676","tag-1672","tag-1674"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/comments?post=719"}],"version-history":[{"count":0,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/719\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/media?parent=719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/categories?post=719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/tags?post=719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}