NSRegExNamedCaptureGroup

The problem we’ve faced

Nearly all modern regular expression engines support numbered capturing groups and numbered backreferences. Long regular expressions with lots of groups and backreferences may be hard to read. They can be particularly difficult to maintain as adding or removing a capturing group in the middle of the regex upsets the numbers of all the groups that follow the added or removed group.

Good news

Languages or libraries like Python, PHP’s preg engine, and .NET languages support captures to named locations, that we called Named Capture Groups. One of the most important benefits of NCG is that assigning a human-readable name to each individual capture group may be less confusing later to someone reading the code who might otherwise be left wondering about which number exactly conrrepsponds which capture group.

(?…) Named capture group. The are
literal – they appear in the pattern.

for example:

b(?

ddd)-(?ddd)-(?dddd)b

However, Apple’s own documentation for NSRegEx does not list the syntax for Named Capture Groups, it only appears on ICU’s
own documentation, suggesting that NCG are a recent
addition and hence Cocoa’s implementation has not integrated it yet.

That is to say, the only way of capturing group matching results exposed by NSRegEx is currently by talking with rangeAt(:_) method within NSTextCheckingResult class, which is number-based. Come on, Cocoa.

Happy are those who are sad … – Matthew 5:4

The extension library, NSRegExNamedCaptureGroup, aims at providing developers using NSRegEx
with an intuitive approach to deal with Named Capture Groups within
their regular expressions.

To install using CocoaPods, add the following to your project Podfile:

pod 'NSRegExNamedCaptureGroup', '~>1.0.0'

Swift Package Manager:

The Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

Once you have your Swift package set up, adding the framework as a dependency is as easy as adding it to the dependencies value of your Package.swift.

let firstMatch = pattern.firstMatch( in: phoneNumber, range: range )
// Much better ...
// ... than invoking `rangeAt( 1 )`
print( NSStringFromRange( firstMatch!.rangeWith( "Area" ) ) )
// prints "{0, 3}"
// ... than putting your program at the risk of getting an
// unexpected result back by passing `rangeAt( 2 )` when you
// forget that the middle capture group (?:ddd) is wrapped
// within a pair of grouping-only parentheses, which means
// it will not participate in capturing at all.
//
// Conversely, in the case of using
// NSRegExNamedCaptureGroup's extension method `rangeWith(:_)`,
// we will only get a range {NSNotFound, 0} when the specified
// group name does not exist in the original regex.
print( NSStringFromRange( firstMatch!.rangeWith( "Exch" ) ) )
// There's no a capture group named as "Exch",
// so prints "{9223372036854775807, 0}"
// ... than invoking `rangeAt( 2 )`
print( NSStringFromRange( firstMatch!.rangeWith( "Num" ) ) )
// prints "{8, 4}"

⚠️

This is an experimental pre-processing to Cocoa’s regex implementation. There’s every likelihood that I’ve broken something or ignored a better option, somehow. Feel free to create an issue on GitHub if you encounter any problems or have a suggestion for a better approach.