Using function builders to create pattern string for regular expression parsing

Though regular expression syntax is well established and many tutorials and reference guides exist, the necessary syntax patterns can be quite cumbersome if someone does only use them from time to time.

The meaning of ^[a-z0-9_-]{3,16}$ for example becomes difficult to understand after some time.

Therefore I though about using the new experimental function builder feature of swift for creation of a more readable DSL.
Don't get me wrong: As a first step, I don't want to write a whole regexp library. I only want to use this DSL to generate regexp patterns.

You would write something like

RegExp {
  repeat(min:3, max:16){

or something similar which should result in "^[a-z0-9_-]{3,16}$" which in turn could be passed on to standard libraries for pattern matching.

As you can see I am not sure about what form the DSL should have and what form is even possible with the function builders in swift and how to represent the regular expression builder components in the swift language. Enums with associated values for the arguments and indirect cases for subexpressions may be possible.

That's why I want to discuss this idea here to get more ideas. Feel free to improve the idea or even "steal" it completely and do something (hopefully an open source package :wink:) with it yourself.

1 Like

You can see some previously-discussed ideas on this area here, though it predates function builders.

Yes, that predates function builders and therefore has a whole different form than what I had in mind, but thanks for the link.