Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hedhyw/rex
Flexible regular expressions constructor for Golang.
https://github.com/hedhyw/rex
builder-pattern constructor dsl dsl-syntax go golang regexp regexp-builder regular-expression regular-expressions
Last synced: 19 days ago
JSON representation
Flexible regular expressions constructor for Golang.
- Host: GitHub
- URL: https://github.com/hedhyw/rex
- Owner: hedhyw
- License: mit
- Created: 2022-06-13T17:30:44.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-12T07:58:31.000Z (2 months ago)
- Last Synced: 2024-11-16T19:02:43.288Z (26 days ago)
- Topics: builder-pattern, constructor, dsl, dsl-syntax, go, golang, regexp, regexp-builder, regular-expression, regular-expressions
- Language: Go
- Homepage:
- Size: 672 KB
- Stars: 196
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - rex - Regular expressions builder. (Text Processing / Regular Expressions)
- my-awesome - hedhyw/rex - pattern,constructor,dsl,dsl-syntax,go,golang,regexp,regexp-builder,regular-expression,regular-expressions pushed_at:2024-10 star:0.2k fork:0.0k Flexible regular expressions constructor for Golang. (Go)
- awesome-go-extra - rex - 06-13T17:30:44Z|2022-07-01T08:26:07Z| (Bot Building / Regular Expressions)
README
# Rex
![Version](https://img.shields.io/github/v/tag/hedhyw/rex)
[![Go Report Card](https://goreportcard.com/badge/github.com/hedhyw/rex)](https://goreportcard.com/report/github.com/hedhyw/rex)
[![Coverage Status](https://coveralls.io/repos/github/hedhyw/rex/badge.svg?branch=main)](https://coveralls.io/github/hedhyw/rex?branch=main)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/hedhyw/rex)](https://pkg.go.dev/github.com/hedhyw/rex?tab=doc)![rex-gopher](_docs/gopher.png)
**This is a regular expressions builder for gophers!**
- **[Why?](#why)**
- **[FAQ](#faq)**
- **[Documentation](_docs/library.md)**
- **[Examples](pkg/rex/examples_test.go)**
- **[License](#license)**## 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`](https://pkg.go.dev/regexp#Regexp).
The library supports [groups](_docs/library.md#groups), [composits](_docs/library.md#groups), [classes](_docs/library.md#character-classes), [flags](_docs/library.md#flags), [repetitions](_docs/library.md#repetitions) and if you want you can even use `raw regular expressions` in any place. Also it contains a set of [predefined helpers](_docs/library.md#helper) with patterns for number ranges, phones, emails, etc...
Let's see an example of validating or matching `someid[#]` using a verbose pattern:
```golang
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:```golang
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.
```golang
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:
```golang
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](https://github.com/hedhyw/rex/issues/new?labels=question&title=I+have+a+question).
## License
- The library is under [MIT Lecense](LICENSE)
- [The gopher](_docs/gopher.png) is under [Creative Commons Attribution 3.0](https://creativecommons.org/licenses/by/3.0/) license. It was originally created by [Renée French](https://en.wikipedia.org/wiki/Ren%C3%A9e_French) and redrawed by me.
- [The meme](_docs/meme.png) contains two frame fragments from [the video](https://www.youtube.com/watch?v=uxpDa-c-4Mc).