Simplified Regular Expressions in Swift [Advanced]

Simplified Regular Expressions in Swift [Advanced]

Hopefully you're somewhat familiar with regular expressions and maybe even have some experience using them in Swift. If not, I wrote an earlier article series about regular expressions in Swift that can ramp you up quickly.

In today's blog post, let's briefly review a scenario that models how powerfully concise regex can be in Swift.

When working with URLs, you might often need to validate and extract specific components for further processing. We'll demonstrate how to use named capture groups in regular expressions to parse a URL and extract specific path components.

The URL and Path Components

Given the URL:

https://dev.swiftkix.com/api/v1/articles/ad11-2f35-8T13-k21Y/comments/c83

we want to extract:

  • the article identifier in the 4th path component: ad11-2f35-8T13-k21Y

  • the comment identifier in the 6th path component: c83

Regular Expression for Parsing

To achieve this, we will use the following regular expression:

#/^/api/v1/articles/(?<articleId>[\w-]+)/comments/(?<commentId>c\d+)$/#

Here's a breakdown:

  • ^/api/v1/articles/: Matches the fixed part at the beginning of the URL path.

  • (?<articleId>[\w-]+): The outer parentheses indicate a capture group. The name of the capture group appears between the ?< and >, which is articleId. The actual value captured is defined by the pattern [\w-]+ which indicates multiple letters, numbers, and hyphens.

  • /comments/: Matches the next fixed part of the URL path.

  • (?<commentId>c\d+): The outer parentheses indicate another capture group. The name of this capture group is commentId. The value captured is defined by the pattern c\d+, which indicates a letter 'c' followed by one or more digits.

  • $: Matches the end of the URL path.

Swift Code Implementation

Let's dive into the Swift code to parse the URL using this regular expression.

// The URL to be parsed
let url = URL(string: "https://dev.sample.com/api/v1/articles/AD11-2F35-8T13-K21Y/comments/c83")!

// The regular expression pattern
let pattern = #/^/api/v1/articles/(?<articleId>[\w-]+)/comments/(?<commentId>c\d+)$/#

// Find matches in the URL
if let match = url.path.firstMatch(of: pattern) {

  // Access the validated values
  let articleId = String(match.output.articleId)
  let commentId = String(match.output.commentId)

  //  ... additional processing ...
}

Conclusion

Using regular expressions to parse URLs and extract specific components can be straightforward and powerful. Named capture groups allow you to isolate and access these components easily. Plus, they're checked at compile time, helping to avoid misspelling errors.

Happy coding!