Skip to content

A macro to replace the character classes from SwiftUtilities in the form `${MY_CHARACTER_CLASS}` in a regular expression text, returning a literal regular expression.

License

Notifications You must be signed in to change notification settings

stefanspringer1/RegexWithCharacterClasses

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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. (The dollar sign and the ampersand can be escaped using a backslash.) You will get a compiler error if you have chosen a non-existing character class or entity name.

To use this macro, you need to know how many groups are in the regular expression and choose the according macro name. If there are <n> groups, you will have to call the macro by #regexWithCharacterClassesAnd<n>Groups(...), e.g #regexWithCharacterClassesAnd2(...) if there are two groups (exception: when having one group, you need to call regexWithCharacterClassesAnd1Group). Currently, you can call the macro for up to seven groups. You will get a compiler error if you have chosen the wrong macro name.

Usage (note that the type annotation is not necessary):

// note that the type annotation is not necessary:
let regex: Regex<Substring> = #regexWithCharacterClassesAnd1Group("[${LATIN_LETTERS}]")
print("123 hello!".replacing(regex, with: "x")) // "123 xxxxx!"

About

A macro to replace the character classes from SwiftUtilities in the form `${MY_CHARACTER_CLASS}` in a regular expression text, returning a literal regular expression.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages