Updated to Xcode 27 and found that a Swift regular expression suddenly hangs when matching text?
There’s a confirmed regression affecting certain RegexBuilder patterns on iOS 27 with Swift 6.4.
The problem occurs when an unbounded repetition contains a pattern that can match an empty string, and that pattern begins with NegativeLookahead.
Here’s a minimal example that can get stuck:
import RegexBuilder
let regex = Regex {
ZeroOrMore {
Capture {
NegativeLookahead { "a" }
ZeroOrMore(.digit)
} transform: { String($0) }
}
}
_ = "A".matches(of: regex)
The matcher can repeatedly process an empty match without moving forward through the input. The result? Your code never finishes.
The Fix
One documented workaround is to wrap the affected capture contents in Optionally:
import RegexBuilder
let digits = Regex {
NegativeLookahead { "a" }
ZeroOrMore(.digit)
}
let regex = Regex {
ZeroOrMore {
Capture {
Optionally {
digits
}
} transform: { String($0) }
}
}
_ = "A".matches(of: regex)
This avoids the infinite loop in the reported reproduction.
There’s one catch: although this workaround preserves the capture’s output type, it can change how often the capture transform runs. If your transform has side effects, check that behaviour before shipping.
Also, this isn’t a fix for every regex that hangs. It specifically addresses the reported combination of NegativeLookahead, an empty-matchable pattern and unbounded repetition.
If your regex worked on earlier iOS versions but hangs on iOS 27, it’s worth checking for that combination before spending hours debugging the surrounding code.
The original report, including the reproduction and workaround, is on the Swift Forums.








