View in English

  • Apple Developer
    • Get Started

    Explore Get Started

    • Overview
    • Learn
    • Apple Developer Program

    Stay Updated

    • Latest News
    • Hello Developer
    • Platforms

    Explore Platforms

    • Apple Platforms
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store

    Featured

    • Design
    • Distribution
    • Games
    • Accessories
    • Web
    • Home
    • CarPlay
    • Technologies

    Explore Technologies

    • Overview
    • Xcode
    • Swift
    • SwiftUI

    Featured

    • Accessibility
    • App Intents
    • Apple Intelligence
    • Games
    • Machine Learning & AI
    • Security
    • Xcode Cloud
    • Community

    Explore Community

    • Overview
    • Meet with Apple events
    • Community-driven events
    • Developer Forums
    • Open Source

    Featured

    • WWDC
    • Swift Student Challenge
    • Developer Stories
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Centers
    • Documentation

    Explore Documentation

    • Documentation Library
    • Technology Overviews
    • Sample Code
    • Human Interface Guidelines
    • Videos

    Release Notes

    • Featured Updates
    • iOS
    • iPadOS
    • macOS
    • watchOS
    • visionOS
    • tvOS
    • Xcode
    • Downloads

    Explore Downloads

    • All Downloads
    • Operating Systems
    • Applications
    • Design Resources

    Featured

    • Xcode
    • TestFlight
    • Fonts
    • SF Symbols
    • Icon Composer
    • Support

    Explore Support

    • Overview
    • Help Guides
    • Developer Forums
    • Feedback Assistant
    • Contact Us

    Featured

    • Account Help
    • App Review Guidelines
    • App Store Connect Help
    • Upcoming Requirements
    • Agreements and Guidelines
    • System Status
  • Quick Links

    • Events
    • News
    • Forums
    • Sample Code
    • Videos
 

Vidéos

Ouvrir le menu Fermer le menu
  • Collections
  • Toutes les vidéos
  • À propos

Plus de vidéos

  • À propos
  • Code
  • Swift Regex: Beyond the basics

    Go beyond the basics of string processing with Swift Regex. We'll share an overview of Regex and how it works, explore Foundation's rich data parsers and discover how to integrate your own, and delve into captures. We'll also provide best practices for matching strings and wielding Regex-powered algorithms with ease.

    Ressources

    • SE-0357: Regex-powered algorithms
    • SE-0355: Regex syntax
    • SE-0354: Regex literals
    • SE-0351: Regex builder DSL
    • SE-0350: Regex type and overview
      • Vidéo HD
      • Vidéo SD

    Vidéos connexes

    WWDC22

    • Meet Swift Regex
    • What's new in Swift

    WWDC21

    • What's new in Foundation
  • Rechercher dans cette vidéo…
    • 0:39 - Regex matching "Hi, WWDC22!"

      Regex {
          "Hi, WWDC"
          Repeat(.digit, count: 2)
          "!"
      }
    • 1:06 - Simple Regex from a string

      let input = "name:  John Appleseed,  user_id:  100"
      
      let regex = try Regex(#"user_id:\s*(\d+)"#)
      
      if let match = input.firstMatch(of: regex) {
          print("Matched: \(match[0])")
          print("User ID: \(match[1])")
      }
    • 1:56 - Simple Regex from a literal

      let input = "name:  John Appleseed,  user_id:  100"
      
      let regex = /user_id:\s*(\d+)/
      
      if let match = input.firstMatch(of: regex) {
          print("Matched: \(match.0)")
          print("User ID: \(match.1)")
      }
    • 2:08 - Simple regex builder

      import RegexBuilder
      
      let input = "name:  John Appleseed,  user_id:  100"
      
      let regex = Regex {
          "user_id:"
          OneOrMore(.whitespace)
          Capture(.localizedInteger)
      }
      
      if let match = input.firstMatch(of: regex) {
          print("Matched: \(match.0)")
          print("User ID: \(match.1)")
      }
    • 2:38 - A trivial Regex interpreted by the Regex engine

      let regex = Regex {
          OneOrMore("a")
          OneOrMore(.digit)
      }
      
      let match = "aaa12".wholeMatch(of: regex)
    • 3:49 - Regex-powered algorithms

      let input = "name:  John Appleseed,  user_id:  100"
      
      let regex = /user_id:\s*(\d+)/
      
      input.firstMatch(of: regex)           // Regex.Match<(Substring, Substring)>
      input.wholeMatch(of: regex)           // nil
      input.prefixMatch(of: regex)          // nil
      
      input.starts(with: regex)             // false
      input.replacing(regex, with: "456")   // "name:  John Appleseed,  456"
      input.trimmingPrefix(regex)           // "name:  John Appleseed,  user_id:  100"
      input.split(separator: /\s*,\s*/)     // ["name:  John Appleseed", "user_id:  100"]
      
      switch "abc" {
      case /\w+/:
          print("It's a word!")
      }
    • 5:14 - Use Foundation parsers in regex builder

      let statement = """
          DSLIP    04/06/20 Paypal  $3,020.85
          CREDIT   04/03/20 Payroll $69.73
          DEBIT    04/02/20 Rent    ($38.25)
          DEBIT    03/31/20 Grocery ($27.44)
          DEBIT    03/24/20 IRS     ($52,249.98)
          """
      
      let regex = Regex {
          Capture(.date(format: "\(month: .twoDigits)/\(day: .twoDigits)/\(year: .twoDigits)"))
          OneOrMore(.whitespace)
          OneOrMore(.word)
          OneOrMore(.whitespace)
          Capture(.currency(code: "USD").sign(strategy: .accounting))
      }
    • 6:24 - XCTest log regex (version 1)

      import RegexBuilder
      
      let regex = Regex {
          "Test Suite '"
          /[a-zA-Z][a-zA-Z0-9]*/
          "' "
          ChoiceOf {
              "started"
              "passed"
              "failed"
          }
          " at "
          OneOrMore(.any)
          Optionally(".")
      }
    • 6:25 - Test our Regex against some inputs

      let testSuiteTestInputs = [
          "Test Suite 'RegexDSLTests' started at 2022-06-06 09:41:00.001",
          "Test Suite 'RegexDSLTests' failed at 2022-06-06 09:41:00.001.",
          "Test Suite 'RegexDSLTests' passed at 2022-06-06 09:41:00.001."
      ]
      
      for line in testSuiteTestInputs {
          if let match = line.wholeMatch(of: regex) {
              print("Matched: \(match.output)")
          }
      }
    • 10:28 - Example of capture

      let regex = Regex {
         "a"
         Capture("b")
         "c"
         /d(e)f/
      }
      
      if let match = "abcdef".wholeMatch(of: regex) {
          let (wholeMatch, b, e) = match.output
      }
    • 11:10 - XCTest log regex (version 2, with captures)

      import RegexBuilder
      
      let regex = Regex {
          "Test Suite '"
          Capture(/[a-zA-Z][a-zA-Z0-9]*/)
          "' "
          Capture {
              ChoiceOf {
                  "started"
                  "passed"
                  "failed"
              }
          }
          " at "
          Capture(OneOrMore(.any))
          Optionally(".")
      }
    • 11:21 - Test our Regex (version 2) against some inputs

      let testSuiteTestInputs = [
          "Test Suite 'RegexDSLTests' started at 2022-06-06 09:41:00.001",
          "Test Suite 'RegexDSLTests' failed at 2022-06-06 09:41:00.001.",
          "Test Suite 'RegexDSLTests' passed at 2022-06-06 09:41:00.001."
      ]
      
      for line in testSuiteTestInputs {
          if let (whole, name, status, dateTime) = line.wholeMatch(of: regex)?.output {
              print("Matched: \"\(name)\", \"\(status)\", \"\(dateTime)\"")
          }
      }
    • 11:51 - XCTest log regex (version 3, with reluctant repetition)

      import RegexBuilder
      
      let regex = Regex {
          "Test Suite '"
          Capture(/[a-zA-Z][a-zA-Z0-9]*/)
          "' "
          Capture {
              ChoiceOf {
                  "started"
                  "passed"
                  "failed"
              }
          }
          " at "
          Capture(OneOrMore(.any, .reluctant))
          Optionally(".")
      }
    • 15:20 - Example of transforming capture

      Regex {
          Capture {
              OneOrMore(.digit)
          } transform: {
              Int($0)     // Int.init?(_: some StringProtocol)
          }
      } // Regex<(Substring, Int?)>
    • 15:55 - Example of transforming capture and removing optionality

      Regex {
          TryCapture {
              OneOrMore(.digit)
          } transform: {
              Int($0)     // Int.init?(_: some StringProtocol)
          }
      } // Regex<(Substring, Int)>
    • 16:21 - XCTest log regex (version 4, with transforming capture)

      enum TestStatus: String {
          case started = "started"
          case passed = "passed"
          case failed = "failed"
      }
      
      let regex = Regex {
          "Test Suite '"
          Capture(/[a-zA-Z][a-zA-Z0-9]*/)
          "' "
          TryCapture {
              ChoiceOf {
                  "started"
                  "passed"
                  "failed"
              }
          } transform: {
              TestStatus(rawValue: String($0))
          }
          " at "
          Capture(OneOrMore(.any, .reluctant))
          Optionally(".")
      } // Regex<(Substring, Substring, TestStatus, Substring)>
    • 17:23 - XCTest log regex (version 5, with Foundation ISO 8601 date parser)

      let regex = Regex {
          "Test Suite '"
          Capture(/[a-zA-Z][a-zA-Z0-9]*/)
          "' "
          TryCapture {
              ChoiceOf {
                  "started"
                  "passed"
                  "failed"
              }
          } transform: {
              TestStatus(rawValue: String($0))
          }
          " at "
          Capture(.iso8601(
              timeZone: .current, includingFractionalSeconds: true, dateTimeSeparator: .space))
          Optionally(".")
      } // Regex<(Substring, Substring, TestStatus, Date)>
    • 18:19 - XCTest log duration parser

      let input = "Test Case '-[RegexDSLTests testCharacterClass]' passed (0.001 seconds)."
      
      let regex = Regex {
          "Test Case "
          OneOrMore(.any, .reluctant)
          "("
          Capture {
              .localizedDouble
          }
          " seconds)."
      }
      
      if let match = input.wholeMatch(of: regex) {
          print("Time: \(match.output)")
      }
    • 19:16 - CDoubleParser definition

      import Darwin
      
      struct CDoubleParser: CustomConsumingRegexComponent {
          typealias RegexOutput = Double
      
          func consuming(
              _ input: String, startingAt index: String.Index, in bounds: Range<String.Index>
          ) throws -> (upperBound: String.Index, output: Double)? {
              input[index...].withCString { startAddress in
                  var endAddress: UnsafeMutablePointer<CChar>!
                  let output = strtod(startAddress, &endAddress)
                  guard endAddress > startAddress else { return nil }
                  let parsedLength = startAddress.distance(to: endAddress)
                  let upperBound = input.utf8.index(index, offsetBy: parsedLength)
                  return (upperBound, output)
              }
          }
      }
    • 20:13 - Use CDoubleParser in regex builder

      let input = "Test Case '-[RegexDSLTests testCharacterClass]' passed (0.001 seconds)."
      
      let regex = Regex {
          "Test Case "
          OneOrMore(.any, .reluctant)
          "("
          Capture {
              CDoubleParser()
          }
          " seconds)."
      } // Regex<(Substring, Double)>
      
      if let match = input.wholeMatch(of: regex) {
          print("Time: \(match.1)")
      }

Developer Footer

  • Vidéos
  • WWDC22
  • Swift Regex: Beyond the basics
  • Open Menu Close Menu
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store
    Open Menu Close Menu
    • Swift
    • SwiftUI
    • Swift Playground
    • TestFlight
    • Xcode
    • Xcode Cloud
    • Icon Composer
    • SF Symbols
    Open Menu Close Menu
    • Accessibility
    • Accessories
    • Apple Intelligence
    • Audio & Video
    • Augmented Reality
    • Business
    • Design
    • Distribution
    • Education
    • Games
    • Health & Fitness
    • In-App Purchase
    • Localization
    • Maps & Location
    • Machine Learning & AI
    • Security
    • Safari & Web
    Open Menu Close Menu
    • Documentation
    • Downloads
    • Sample Code
    • Videos
    Open Menu Close Menu
    • Help Guides & Articles
    • Contact Us
    • Forums
    • Feedback & Bug Reporting
    • System Status
    Open Menu Close Menu
    • Apple Developer
    • App Store Connect
    • Certificates, IDs, & Profiles
    • Feedback Assistant
    Open Menu Close Menu
    • Apple Developer Program
    • Apple Developer Enterprise Program
    • App Store Small Business Program
    • MFi Program
    • Mini Apps Partner Program
    • News Partner Program
    • Video Partner Program
    • Security Bounty Program
    • Security Research Device Program
    Open Menu Close Menu
    • Meet with Apple
    • Apple Developer Centers
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Academies
    • WWDC
    Read the latest news.
    Get the Apple Developer app.
    Copyright © 2026 Apple Inc. All rights reserved.
    Terms of Use Privacy Policy Agreements and Guidelines