Swiftpack.co - ULazdins/BeRegEx as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by ULazdins.
ULazdins/BeRegEx 0.0.1
Better Regular Expressions
⭐️ 9
🕓 3 years ago
.package(url: "https://github.com/ULazdins/BeRegEx.git", from: "0.0.1")

BeRegEx

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.

Goal

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

"^([A-Z0-9a-z._%+-]+)@([A-Za-z0-9.-]+\\.[A-Za-z]{2,64})$"

with

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

let domainRegex = AnyOf(.alphanumerics, .custom(".-")).oneOrMore() +
    Exact(".") +
    AnyOf(.letters).between(2...64)

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

then the regex can be used:

let doesntMatch = regex.match("[email protected]")
print(doesntMatch) // prints []

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

Installation

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: [
    .target( 
        name: "YourTarget", 
        dependencies: [
            "BeRegEx"
        ]
    ),
    ...
]
...

Operators

AnyOf

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.

Examples:

AnyOf(.letters).zeroOrMore()

AnyOf(.letters).oneOrMore()

AnyOf(.letters).between(1...4)

Exact

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

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

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"

GitHub

link
Stars: 9
Last commit: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

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