Some regex tools

The following packages of mine concerning regex expressions might be useful for some people:

  • RegexWithDescription: "This macro makes is possible to have both a regex built from a literal regex expression and the original regex formulation as text without repeating the regex formulation in the code." (cf. Regex Type: Retrieve String Pattern)
  • RegexTemplates: "This package makes it possible to efficiently replace a regular expression with a template containing $1 , S2 , etc."
  • RegexTemplatesWithEntities: " This package makes it possible to efficiently replace a regular expression with a template containing $1, S2, etc. Other than with the RegexTemplates package, character entities in the template of the (XML notation) form &MY_CHARACTER_ENTITY;, will be replaced by the according text."
  • RegexWithCharacterClasses: "A macro to replace the character classes from SwiftUtilities in the form ${MY_CHARACTER_CLASS} or the usual character entities the (XML notation) form &MY_CHARACTER_ENTITY; in a regular expression text, returning a literal regular expression."

As replacing regex expressions is still quite slow in Swift, you might also have a look at:

  • FastReplace: "This package offers an efficient way to replace certain Unicode code points (either represented by UnicodeScalar or UInt32 ) with other values. Those methods might be more efficient than the replacement methods of the Swift standard library..."

In some of those packages you have to use versions of the macro or also versions of a struct according to the number of (non-optional) groups in the regex. The type system of Swift in the current state does not seem to allow a better solution...

Of course I would wish that at least the first and the last of the above packages would not be necessary, that the available implementations that come with Swift would suffice.

2 Likes

Thanks, I see immediate application in my html library.