I am looking at the RegexBuilder api from the point of newcomers, and this one looks confusing:
import RegexBuilder
let s = "a\nb"
let rx = Regex {
Anchor.startOfLine
"b"
}
s.firstMatch(of: rx) // nil
There is no match, and the fix is:
let s = "a\nb"
let rx = Regex {
Anchor.startOfLine
"b"
}
.anchorsMatchLineEndings()
s.firstMatch(of: rx) // ["b"]
It seems to me this was influenced by the legacy regex syntax, whereas it should be the other way round: the difference between .startOfLine and .startOfSubject is already unambiguous, and should not require a mode modifier.
This option applies only to anchors used in a regex literal. The anchors defined in RegexBuilder are specific about matching at the start/end of the input or the line, and therefore do not correspond directly with the ^ and $ literal anchors.
But the intended behavior is not implemented in current release.
Xcode version 14.0 beta (14A5228q)
swift -version
swift-driver version: 1.55.1 Apple Swift version 5.7 (swiftlang-5.7.0.113.202 clang-1400.0.16.2)