Swift extract regex matches
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Swift, the classic way to extract regex matches is NSRegularExpression. The main detail to get right is that the regex engine uses NSRange, while Swift strings use their own Unicode-aware index model, so safe range conversion is part of the job.
Basic Match Extraction with NSRegularExpression
A straightforward example looks like this:
This prints the matched numeric substrings. The important step is converting match.range into a Swift range before slicing the string.
Why the Range Conversion Matters
Swift strings are Unicode-correct and do not use plain integer indexing. NSRegularExpression, however, is a Foundation API based on NSRange, which uses UTF-16 offsets.
That means this conversion is not optional ceremony:
It is the safe bridge between Foundation regex results and Swift string slicing.
Extract Capture Groups
Many regex tasks are really about groups rather than the full match.
Remember that group 0 is the full match, and group 1 onward are your explicit capture groups.
Wrap It in a Helper
If you do this often, a helper function makes the code much cleaner.
This keeps the Foundation bridging details out of your calling code.
Extracting Specific Groups with a Helper
You can also build a helper that returns captured groups.
This is useful when you want structured extracted data instead of only the matched surface text.
Newer Swift Regex APIs Exist Too
Modern Swift also includes native regex features, but NSRegularExpression remains common in real codebases because it is widely available, interoperates with Foundation easily, and shows up in older projects.
So even if you later adopt newer regex syntax, understanding the Foundation approach is still practical.
Common Pitfalls
The biggest mistake is trying to use raw NSRange values directly to index a Swift string. That will break because Swift strings are not simple byte arrays.
Another issue is forgetting that capture group 0 is the entire match, not the first explicit captured value.
Developers also recompile the same regex repeatedly in hot paths instead of creating it once and reusing it.
Finally, do not blame the extraction logic when the real problem is an overly broad or poorly anchored regex pattern.
Summary
- '
NSRegularExpressionis a standard way to extract regex matches in Swift.' - Convert
NSRangeresults into Swift ranges before slicing strings. - Use capture groups when you need subparts of a match.
- Wrap common regex extraction patterns in helper functions for readability.
- Keep the regex pattern itself disciplined, because extraction quality depends on it.
Related reading
- Swift Framework Umbrella header '....h' not found
- Swift Framework Umbrella header '....h' not found
- Swift GET request with parameters
- Swift GET request with parameters
- Swift guard let vs if let
- Swift How to get substring from start to last index of character
- Swift How to get substring from start to last index of character
- Swift How to refresh UICollectionView layout after rotation of the device
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.