We can convert a regex literal to a RegexBuilder pattern using Xcode, but is there way to access the generated regex literal from a RegexBuilder pattern?
This would be useful in figuring out how different RegexBuilder components map to the original regex string syntax.
For example, I'm trying to understand how the Optionally component works and being able to see the string it generates would help me figure out what is happening behind the scenes.
RegexBuilder does not generate textual regex internally, and there are some features that could not be represented in a textual regex. This feature is in the plans though, as mentioned here SE-0351: Regex Builder DSL - #13 by rxwei
I am learning RegexBuilder features by feeding textual regexes to the interactive generator of mine here RegexBuilder Generator
@paiv already said this in his answer, but I want to spell it out more clearly: there is no "generated textual regex" from a builder. "Classic" regexes and builders are two source representations of programs to be executed by the matching engine. Both the classic regex representation and the builder are consumed by the Swift compiler to produce an intermediate representation of the program. The actual programs supported by the matching engine are a superset of what can be encoded in the two representations.
The generator tool doesn't generate the correct Regex builder for the email address pattern: "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
the resulting builder fails to validate valid emails like: test@gmail.com
Just tested this with the latest version of the library, and it produces correct builder syntax for me, which matches valid emails. What version are you using where you're seeing incorrectly generated builder code?
I used the latest version, but my mistake was supplying the regex with escape strings as @paiv pointed out above.
Anyway, off topic, I found out as a best practice is not to use regex to validate emails, but rather we can use NSDataDetector, mentioned in in this cool article.
Another helpful tool worth trying is swiftregex.com
It's a playground for evaluating Swift Regex's and also converts from Swift Regex literals to RegexBuilder (but not back).