• Stars
    star
    194
  • Rank 200,219 (Top 4 %)
  • Language
    Go
  • License
    MIT License
  • Created over 2 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Flexible regular expressions constructor for Golang.

Rex

Version Go Report Card Coverage Status PkgGoDev

rex-gopher

This is a regular expressions builder for gophers!

Why?

It makes readability better and helps to construct regular expressions using human-friendly constructions. Also, it allows commenting and reusing blocks, which improves the quality of code. It provides a convenient way to use parameterized patterns. It is easy to implement custom patterns or use a combination of others.

It is just a builder, so it returns standart *regexp.Regexp.

The library supports groups, composits, classes, flags, repetitions and if you want you can even use raw regular expressions in any place. Also it contains a set of predefined helpers with patterns for number ranges, phones, emails, etc...

Let's see an example of validating or matching someid[#] using a verbose pattern:

re := rex.New(
    rex.Chars.Begin(), // `^`
    // ID should begin with lowercased character.
    rex.Chars.Lower().Repeat().OneOrMore(), // `[a-z]+`
    // ID should contain number inside brackets [#].
    rex.Group.NonCaptured( // (?:)
        rex.Chars.Single('['),                   // `[`
        rex.Chars.Digits().Repeat().OneOrMore(), // `[0-9]+`
        rex.Chars.Single(']'),                   // `]`
    ),
    rex.Chars.End(), // `$`
).MustCompile()

Yes, it requires more code, but it has its advantages.

More, but simpler code, fewer bugs.

You can still use original regular expressions as is in any place. Example of matching numbers between -111.99 and 1111.99 using a combination of patterns and raw regular expression:

re := rex.New(
    rex.Common.Raw(`^`),
    rex.Helper.NumberRange(-111, 1111),
    rex.Common.RawVerbose(`
        # RawVerbose is a synonym to Raw,
        # but ignores comments, spaces and new lines.
        \.        # Decimal delimter.  
        [0-9]{2}  # Only two digits.
        $         # The end.
    `),
).MustCompile()

// Produces:
// ^((?:\x2D(?:0|(?:[1-9])|(?:[1-9][0-9])|(?:10[0-9])|(?:11[0-1])))|(?:0|(?:[1-9])|(?:[1-9][0-9])|(?:[1-9][0-9][0-9])|(?:10[0-9][0-9])|(?:110[0-9])|(?:111[0-1])))\.[0-9]{2}$

The style you prefer is up to you.

Meme

Drake Hotline Bling meme

FAQ

  1. It is too verbose. Too much code.

    More, but simpler code, fewer bugs. Anyway, you can still use the raw regular expressions syntax in combination with helpers.

    rex.New(
        rex.Chars.Begin(),
        rex.Group.Define(
            // `Raw` can be placed anywhere in blocks.
            rex.Common.Raw(`[a-z]+\d+[A-Z]*`),
        ),
        rex.Chars.End(),
    )

    Or just raw regular expression with comments:

    rex.Common.RawVerbose(`
        ^                # Start of the line.
        [a-zA-Z0-9]+     # Local part.
        @                # delimeter.
        [a-zA-Z0-9\.]+   # Domain part.
        $                # End of the line.
    `)
  2. Should I know regular expressions?

    It is better to know them in order to use this library most effectively. But in any case, it is not strictly necessary.

  3. Is it language-dependent? Is it transferable to other languages?

    We can use this library only in Go. If you want to use any parts in other places, then just call rex.New(...).String() and copy-paste generated regular expression.

  4. What about my favourite DSL?

    Every IDE has convenient auto-completion for languages. So all helpers of this library are easy to use out of the box. Also, it is easier to create custom parameterized helpers.

  5. Is it stable?

    It is 0.X.Y version, but there are some backward compatibility guarantees:

    • rex.Chars helpers can change output to an alternative synonym.
    • rex.Common helpers can be deprecated, but not removed.
    • rex.Group some methods can be deprecated.
    • rex.Helper can be changed with breaking changes due to specification complexities.
    • The test coverage should be ~100% without covering test helpers and cmd.
    • Any breaking change will be prevented as much as possible.

    All of the above may not be respected when upgrading the major version.

  6. I have another question. I found an issue. I have a feature request. I want to contribute.

    Please, create an issue.

License

More Repositories

1

gherkingen

Behaviour Driven Development tests generator for Golang
Go
72
star
2

json-log-viewer

Interactive viewer for JSON logs.
Go
71
star
3

spice3f5

Add cmake to spice3f5 for build in linux or windows. SPICE is a general-purpose circuit simulator with several built-in semiconductor device models. SPICE was developed at the Electronics Research Laboratory of the University of California, Berkeley.
C
36
star
4

BrillouinZones

Programs for constructing Brillouin zones in three- and two-dimensional space.
Python
28
star
5

otelinji

OpenTelemetry auto-instrumentation tool for Golang. It automatically injects OpenTelemetry blocks.
Go
16
star
6

go-import-lint

Golang source code analyzer that checks imports order. It verifies that standard, current package, and vendor imports are separated by a line.
Go
16
star
7

Go-Serial-Detector

A go library for determining active serial ports.
Go
14
star
8

jsoncjson

JSONC (json with comments) to JSON translator for Golang.
Go
11
star
9

Directory

Directory & File Chooser dialog for Android
Java
6
star
10

semerr

A way of dealing with Golang errors
Go
6
star
11

HyperFractal

A three-dimensional fractal animation generator.
C++
6
star
12

DinoMega8

Firmware of the Dino game for microcontroller ATmega8
C
4
star
13

DielectricBreakdown

Source codes of programs for a designed device. The device measures the parameters of dielectric films.
C
3
star
14

CExpr

Math Complex Expression Compiler
Java
3
star
15

go-ipbot

Hello-Telegram-Bot that sends the IP address of a remote machine
Go
3
star
16

beehivedlna

Webpage
CSS
2
star
17

LogNotifierBot

Bot for the telegram that sends new messages from sshd/auth.log.
Java
2
star
18

Ocmoxa-Vietblog

The blog with Vietnamese stories for language learners. ๐ŸŒด ๐Ÿ‡ป๐Ÿ‡ณ ๐Ÿฅฅ
HTML
2
star
19

go-psw

A tiny secure-random password generator
Go
2
star
20

telegram-pictionary-it

Telegram MiniApp bot game in which players can draw and guess pictures
Makefile
2
star
21

simple-4bit-cpu

Vivado project with example of simple 4bit CPU
Verilog
2
star
22

Vietnamese-Notes

The blog with brief Vietnamese language notes and an awesome Vietnamese resources list
HTML
1
star
23

Right-Angle-Build

Right Angle Website
HTML
1
star
24

telegram-pictionary-it-backend

Backend for "Pictionary It telegram" bot
Go
1
star
25

telegram-mic-guitar-tuner

@micguitartunerbot is a Telegram Mini App that helps to tune guitars
JavaScript
1
star
26

bdd-resizer-example

JPEG image resizer server for a Golang article
Go
1
star
27

go-tarantool-structure

Go
1
star
28

ippserver-docker

A small docker image which pretends to be a printer.
Makefile
1
star
29

telegram-pictionary-it-frontend

Frontend for "Pictionary It telegram" bot
JavaScript
1
star
30

vscode-golang-gherkingen

VS-Code extension to generate Golang Behaviour Driven Development (BDD) tests
TypeScript
1
star
31

homebrew-main

Collection of homebrews
Ruby
1
star