ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - hedhyw/rex: Flexible regular expressions constructor for Golang.
Flexible regular expressions constructor for Golang. - hedhyw/rex
Visit Site

GitHub - hedhyw/rex: Flexible regular expressions constructor for Golang.

GitHub - hedhyw/rex: 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

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?

    Yes, starting with version v1.0.0.

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

    Please, create an issue.

License

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory