Explaining Regex Locally with Xcode
28 Sep 2025Crafting regular expressions is fun - when you nail the incantation you feel like a wizard but as time passes, that once elegant spell can start to look like the output of an Enigma machine. My approach when needing to figure out what an old regex was doing was to paste it into whatever the top search result for “Explain regular expression” was.
Something that I’ve been doing for a while but hadn’t really thought about was using Xcode’s Refactor
> Convert to Regex Builder
as a way to explain a regular expression without having data leave my machine.
Yes, it’s a shocker but one of Xcode’s refactoring tools actually works for me.
It works surprisingly well and as a bonus gets those endorphins flowing knowing that my data is safe from some AI drivel.
Here’s what this looks like in practice using an example of decoding a regex for UK postcodes taken from this Stackoverflow post.
let postcodeRegex = /^([a-zA-Z]{1,2}[a-zA-Z\d]{1,2})\s(\d[a-zA-Z]{2})$/
After running Refactor
> Convert to Regex Builder
we get
Regex {
/^/
Capture {
Regex {
Repeat(1...2) {
CharacterClass(
("a"..."z"),
("A"..."Z")
)
}
Repeat(1...2) {
CharacterClass(
("a"..."z"),
("A"..."Z"),
.digit
)
}
}
}
One(.whitespace)
Capture {
Regex {
One(.digit)
Repeat(count: 2) {
CharacterClass(
("a"..."z"),
("A"..."Z")
)
}
}
}
/$/
}
Depending on your level of experience with regex you might think this is actually more wordy/overkill (in this case I’d agree) but the point is each part of the regex is broken out into smaller parts that have names explaining what they are doing.
Normally concepts like Capture
, Repeat
and CharacterClass
require you to understand regex syntax but the builder makes them much more approachable.
With the builder style a dev with no experience with regex could make sense of this without needing to look up regex rules and how they are applied.
Wrap up
Even if it wasn’t designed as an “explain this regex” feature, that’s exactly how I use it and it’s a lifesaver when revisiting old code.