Conversion to textual regex is a possible future direction, but I think the functionality is purely additive and therefore is out of scope for this proposal.
I added a future direction section about this in apple/swift-evolution#1612.
Future directions
Conversion to textual regex
Sometimes it may be useful to convert a regex created using regex builder to textual regex. This may be achieved in the future by extending RegexComponent
with a computed property.
extension RegexComponent {
public func makeTextualRegex() -> String?
}
It is worth noting that the internal representation of a Regex
is not textual regex, but an efficient pattern matching bytecode compiled from an abstract syntax tree. Moreover, not every Regex
can be converted to textual regex. Regex builder supports arbitrary types that conform to the RegexComponent
protocol, including CustomMatchingRegexComponent
(pitched in [String Processing Algorithms]) which can be implemented with arbitrary code. If a Regex
contains a CustomMatchingRegexComponent
, it cannot be converted to textual regex.