Swiftpack.co -  ULazdins/BeRegEx as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Better Regular Expressions
.package(url: "https://github.com/ULazdins/BeRegEx.git", from: "0.0.1")


A Better way to do Regular Expressions.

An experimental micro library to improve the readability of regular expressions.

Doesn't have all functions that regular expressions can do and has poor test coverage.


Regular expressions are really hard to read. This library proposes type safe DSL to generate the regular expressions from high level operators.

The proposed DSL would replace



let usernameRegex = AnyOf(.alphanumerics, .custom("._%+-"))

let domainRegex = AnyOf(.alphanumerics, .custom(".-")).oneOrMore() +
    Exact(".") +

let regex = Start() +
    usernameRegex.capture() +
    Exact("@") +
    domainRegex.capture() +

then the regex can be used:

let doesntMatch = regex.match("not.a.valid.because.domain@gmail.com-")
print(doesntMatch) // prints []

let doesMatch = regex.match("is.a.valid.email@gmail.com")
print(doesMatch) // prints [match: "is.a.valid.email@gmail.com", captures: ["is.a.valid.email", "gmail.com"]]


Swift Package Manager

To install it, simply add the dependency to your Package.Swift file:

dependencies: [
    .package(url: "https://github.com/ULazdins/BeRegEx.git", from: "0.0.1"),
targets: [
        name: "YourTarget", 
        dependencies: [



AnyOf replaces the square brackes. Instead of "[a-A]" you would write AnyOf(.lowercaseLetters).

Letter classes can be combined.

AnyOf(.letters, .numbers, .custom("._%+-")) will yield the equivalent of "[A-Z0-9a-z._%+-]"

ZeroOrMore, OneOrMore, Between

To specify the number of repetitions, you can use any of these operators.






Exact replaces exact matches. If you want to match the string "cat", you must now use Exact("cat")

Start, End

Start and End are equivalents of $ and ^ symbols in regular expressions. For example

Exact("aaa") will match a substring of baaab, but Start() + Exact("aaa") + End() will not as there are b before and after aaa.


And concatenates other operators. Library comes with a + operator override so that the usage of And becomes intuitive.

And(reg1: AnyOf(.letters), reg2: Exact("cat"))

is equivalent to AnyOf(.letters) + Exact("cat")

is equivalent to "[a-zA-Z]cat"


Capture creates a capture group.

Exact("<") + AnyOf(.letters).oneOrMore().capture() + Exact(">") will match "<h1>" and <img style="max-width:100%;"> and it will also capture the substring that is matched by AnyOf and oneOrMore, namely "h1" and "img"


Stars: 9
Last commit: 21 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API