{"id":36187,"date":"2022-06-10T17:02:33","date_gmt":"2022-06-10T21:02:33","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=36187"},"modified":"2022-12-02T16:09:13","modified_gmt":"2022-12-02T21:09:13","slug":"swift-regex","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/06\/10\/swift-regex\/","title":{"rendered":"Swift Regex"},"content":{"rendered":"<p><a href=\"https:\/\/developer.apple.com\/videos\/play\/wwdc2022\/110357\/\">Meet Swift Regex<\/a>:<\/p>\n<blockquote cite=\"https:\/\/developer.apple.com\/videos\/play\/wwdc2022\/110357\/\"><p>Learn how you can process strings more effectively when you take advantage of Swift Regex. Come for concise literals but stay for Regex builders &mdash; a new, declarative approach to string processing. We&rsquo;ll also explore the Unicode models in String and share how Swift Regex can make Unicode-correct processing easy.<\/p><\/blockquote>\n\n<p>I&rsquo;m really excited about Swift Regex. It should be much more ergonomic than <code>NSRegularExpression<\/code>, which has always been awkward to use, and faster, too, since it can work directly in Swift&rsquo;s native string encoding.<\/p>\n\n<p>Also, in theory it should handle Unicode edge cases better. I&rsquo;ve run into problems where <code>NSRegularExpression<\/code> returns capture ranges that are valid for <code>NSString<\/code> and <code>String.UTF16View<\/code> but which do not exactly map to valid indexes into the <code>String<\/code> itself.<\/p>\n\n<p>I&rsquo;d like to see Apple bring Swift Regex to Core Data, too. First, it would be nice to get consistent results (both matching and performance characteristics) by using the same engine throughout an app. Second, regex matching within SQLite queries is currently slow because it converts each database string from UTF-8 to UTF-16 before invoking ICU.<\/p>\n\n<p><a href=\"https:\/\/developer.apple.com\/videos\/play\/wwdc2022\/110358\/\">Swift Regex: Beyond the basics<\/a>:<\/p>\n<blockquote cite=\"https:\/\/developer.apple.com\/videos\/play\/wwdc2022\/110358\/\"><p>Go beyond the basics of string processing with Swift Regex. We&rsquo;ll share an overview of Regex and how it works, explore Foundation&rsquo;s rich data parsers and discover how to integrate your own, and delve into captures. We&rsquo;ll also provide best practices for matching strings and wielding Regex-powered algorithms with ease.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/RonAvitzur\/status\/1534616096254242817\">Ron Avitzur<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/RonAvitzur\/status\/1534616096254242817\">\n<p>Tis nifty watching Xcode refactor <code>\/(([0-9]*\\.?[0-9]+)([eE][-+]?[0-9]+)?)|NaN\/<\/code> to a Regex Builder, even if the compiler is unable to type-check this expression in reasonable time.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/stephentyrone\/status\/1534161974052982785\">Steve Canon<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/stephentyrone\/status\/1534161974052982785\">\n<p>The converter that Xcode uses for regex -&gt; builder is in the experimental-string-processing repo.<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/03\/14\/fastscripts-3-1\/\">FastScripts 3.1<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2021\/10\/01\/swift-declarative-string-processing-pitch\/\">Swift Declarative String Processing Pitch<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2019\/03\/21\/utf-8-string-in-swift-5\/\">UTF-8 String in Swift 5<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2016\/12\/19\/nsregularexpression-and-swift\/\">NSRegularExpression and Swift<\/a><\/li>\n<\/ul>\n\n<p id=\"swift-regex-update-2022-06-16\">Update (2022-06-16): <a href=\"https:\/\/twitter.com\/depth42\/status\/1536720077512392705\">Frank Illenberger<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/depth42\/status\/1536720077512392705\"><p>I&rsquo;m disappointed by the performance of the new Swift Regex. Take a look at these two versions of a CSS variable lookup. On my Intel iMac, the first one with <code>NSRegularExpression<\/code> takes 0.0002s to complete an enum over all matches on a 2000 line CSS file. The second one takes 0.25s.<\/p><\/blockquote>\n\n<p id=\"swift-regex-update-2022-10-11\">Update (2022-10-11): <a href=\"https:\/\/useyourloaf.com\/blog\/getting-started-with-swift-regex\/\">Keith Harrison<\/a>:<\/p>\n<blockquote cite=\"https:\/\/useyourloaf.com\/blog\/getting-started-with-swift-regex\/\">\n<p>You can improve the type safety of captured values by adding a transform block to the capture. This allow you to transform the generic capture output to a known type. For example, to transform the captured digits to an (optional) integer[&#8230;]<\/p>\n<\/blockquote>\n<p>And you can use <code>TryCapture<\/code> to make the regex backtrack if the Swift code returns <code>nil<\/code>.<\/p>\n<blockquote cite=\"https:\/\/useyourloaf.com\/blog\/getting-started-with-swift-regex\/\">\n<p>You can use the Foundation date, number, currency and URL parses with regex builder.<\/p>\n<\/blockquote>\n\n<p id=\"swift-regex-update-2022-12-02\">Update (2022-12-02): <a href=\"https:\/\/bignerdranch.com\/blog\/swift-regex\/\">Shane Crawford<\/a>:<\/p>\n<blockquote cite=\"https:\/\/bignerdranch.com\/blog\/swift-regex\/\">\n<p>We&rsquo;ll be digging into Regex Builder to discover its wide-reaching capabilities.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/mastodon.social\/@sindresorhus\/109421294637037232\">Sindre Sorhus<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mastodon.social\/@sindresorhus\/109421294637037232\">\n<p>The new regex stuff in Swift is truly amazing! The following would have been an unreadable mess in most other languages.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Meet Swift Regex: Learn how you can process strings more effectively when you take advantage of Swift Regex. Come for concise literals but stay for Regex builders &mdash; a new, declarative approach to string processing. We&rsquo;ll also explore the Unicode models in String and share how Swift Regex can make Unicode-correct processing easy. I&rsquo;m really [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2022-06-10T21:02:37Z","apple_news_api_id":"9746e3e5-5ae7-4639-80a6-3b12461bfae0","apple_news_api_modified_at":"2022-12-02T21:09:17Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAg==","apple_news_api_share_url":"https:\/\/apple.news\/Al0bj5VrnRjmApjsSRhv64A","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,109,31,2185,46,30,2223,74,138,71,234,425,901,258,226],"class_list":["post-36187","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-coredata","tag-ios","tag-ios-16","tag-languagedesign","tag-mac","tag-macos-13-ventura","tag-opensource","tag-optimization","tag-programming","tag-regex","tag-sqlite","tag-swift-programming-language","tag-unicode","tag-xcode"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=36187"}],"version-history":[{"count":5,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36187\/revisions"}],"predecessor-version":[{"id":37819,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36187\/revisions\/37819"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=36187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=36187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=36187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}