Compile Go regular expressions to Go code

regexp2go

regexp2go is an alternate backend for the regexp package that allows to perform ahead-of-time compilation of regular expressions to Go code.

This is similar in spirit to re2go, but aiming for compatibility with the Go regexp package.

⚠️ This is an experimental PoC that currently implements only a subset of the functionalities and optimizations present in the full regexp package. Rather importantly while it aims at offering the "run in time linear in the size of the input" guarantee that is offered by the regexp package, there may be cases in which this is not currently possible. It may also contain some correctness bugs since the test suite is not really extensive yet.

Using code generated by this package in production is not yet recommended.

Usage

For a quick demo you can use the online demo, or run it locally:

$ go get github.com/CAFxX/regexp2go

# Compile the provided regular expression using the default Go regexp 
# flags (212) and save the result in gen/main.go.
# This requires regexp2go to be on your PATH.
$ regexp2go '(?m)^INFO res=([0-9]+) msg=(.*)' > gen/main.go

# The generated file contains a main function that simply calls the
# generated `Match` function (the one that implements the regular expression)
# passing the input text provided as argument.
$ go run gen/main.go 'INFO res=42 msg=ok'
0: "INFO res=42 msg=ok"
1: "42"
2: "ok"

To generate a Match function that can be called from your program:

# Compile the provided regular expression using the default Go regexp 
# flags (212) and save the result in re/info_line.go.
# The pkg flag specifies the package name. The fn flag specifies the
# name of the generated function.
# This requires regexp2go to be on your PATH.
$ regexp2go -pkg re -fn MatchInfoLine '(?m)^INFO res=([0-9]+) msg=(.*)' > re/info_line.go

Examples

Examples of generated code are in examples/.

Each example contains the generated Go code, and the disassembly of the function that implements the regular expression.

For demo purposes you can also use the online demo to compile a custom regular expression.

Benchmarks

⚠️ These are preliminary results since not all features are implemented, and some edge cases are not handled yet. At the same time, there are many possible optimizations that haven't been implemented yet. See the TODOs in the code for details.

Benchmark results

regexp2go can be from slightly faster to over 5 times faster than the regexp package in the golang standard library, depending on the regexp and the input data.

Owner
Carlo Alberto Ferraris
Carlo Alberto Ferraris
Similar Resources

Converts NFAs (and DFAs) to a regular expressions using the state removal method

nfa2regex: convert NFAs (and DFAs) to regular expressions An implementation of the state removal technique for converting an NFA to a regular expressi

Apr 29, 2022

Quickly cross-compile your C code

Quickly cross-compile your C code

WORK IN PROGRESS: Viceroy is a very early work in progress and is subject to breaking changes. It's also subject to not really working all that well y

Aug 19, 2022

A command-line runner for Go expressions

grun grun is a command-line tool for evaluating Go expressions. It was inspired by runner which does the same for Rust. It constructs a little Go prog

Feb 10, 2022

A simple evaluator for arithmetic integer expressions.

The expr package provides a simple evaluator for arithmetic integer expressions. The syntax and operations are the same as in Go. Operands are the nat

Dec 13, 2022

A commandline tool to resolve URI Templates expressions as specified in RFC 6570.

URI Are you tired to build, concat, replace URL(s) (via shell scripts sed/awk/tr) from your awesome commandline pipeline? Well! here is the missing pi

Jun 9, 2021

Transform PromQL Expressions on the fly

promql-transform Transforms PromQL expressions on the fly Usage Given the expression job:request_latency_seconds:mean5m{job=\"myjob\"} 0.5 Running

Jun 21, 2022

A small & fast dependency-free library for parsing micro expressions.

MicroExpr A small & fast dependency-free library for parsing micro expressions. This library was originally built for use in templating languages (e.g

Nov 25, 2022

Monitoring changes in the source file and automatically compile and run (restart).

Monitoring changes in the source file and automatically compile and run (restart).

dogo Monitoring changes in the source file and automatically compile and run (restart). 中文 Install go get github.com/liudng/dogo Create config Here's

Dec 28, 2022

Write your SQL queries in raw files with all benefits of modern IDEs, use them in an easy way inside your application with all the profit of compile time constants

About qry is a general purpose library for storing your raw database queries in .sql files with all benefits of modern IDEs, instead of strings and co

Dec 25, 2022

An extremely fast Go (golang) HTTP router that supports regular expression route matching. Comes with full support for building RESTful APIs.

ozzo-routing You may consider using go-rest-api to jumpstart your new RESTful applications with ozzo-routing. Description ozzo-routing is a Go package

Dec 31, 2022

Monitoring changes in the source file and automatically compile and run (restart).

Monitoring changes in the source file and automatically compile and run (restart).

dogo Monitoring changes in the source file and automatically compile and run (restart). 中文 Install go get github.com/liudng/dogo Create config Here's

Dec 28, 2022

A dead simple, no frills Go cross compile tool

Gox - Simple Go Cross Compilation Gox is a simple, no-frills tool for Go cross compilation that behaves a lot like standard go build. Gox will paralle

Jan 1, 2023

rxscan provides functionality to scan text to variables using regular expression capture group.

rxscan rxscan provides functionality to scan text to variables using regular expression capture group. This library is still experimental, use at your

Dec 21, 2020

Compile-time dependency injection for Go

Dihedral Dihedral is a compile-time injection framework for Go. Getting started go get -u github.com/dimes/dihedral Create a type you want injected

Jun 1, 2022

chkb turns a regular keyboard into a fully programmable keyboard. It is a cheap programmable keyboard

chkb chkb turns a regular keyboard intro a fully programmable keyboard. So you basically get a cheap programmable keyboard. It has been inspired by QM

Oct 2, 2022

Go compiler made from scratch, which can compile itself. It's going to be the smallest and simplest go compiler in the world.

Babygo, a go compiler made from scratch Babygo is a small and simple go compiler. (Smallest and simplest in the world, I believe.) It is made from scr

Jan 8, 2023

Compile-time Dependency Injection for Go

Wire: Automated Initialization in Go Wire is a code generation tool that automates connecting components using dependency injection. Dependencies betw

Jan 2, 2023

Qt binding for Go (Golang) aims get Go's compile speed again.

qt.go Qt5 binding for Go (Golang) without CGO that aims to achieve Go's native compile speeds. Instead of using common bindings and heavy C++ wrapper

Dec 19, 2022

A CLI tool that you can use create regular backups of your Notion.so Pages.

notion-offliner A CLI tool that you can use create regular backups of your Notion.so Pages. Perfect for disaster scenarios and offline usage. MacOS an

Jan 3, 2023
Transpiling fortran code to golang code

f4go Example of use > # Install golang > # Compile f4go > go get -u github.com/Konstantin8105/f4go > cd $GOPATH/src/github.com/Konstantin8105/f4go > g

Sep 26, 2022
A compiler from Go to JavaScript for running Go code in a browser

GopherJS - A compiler from Go to JavaScript GopherJS compiles Go code (golang.org) to pure JavaScript code. Its main purpose is to give you the opport

Dec 30, 2022
Grumpy is a Python to Go source code transcompiler and runtime.

Grumpy: Go running Python Overview Grumpy is a Python to Go source code transcompiler and runtime that is intended to be a near drop-in replacement fo

Jan 7, 2023
Transform Go code into it's AST

Welcome to go2ast ?? Transform Go code into it's AST Usage echo "a := 1" | go run main.go Example output []ast.Stmt { &ast.AssignStmt {

Dec 13, 2022
Syntax-aware Go code search, based on the mvdan/gogrep
Syntax-aware Go code search, based on the mvdan/gogrep

gogrep WIP: this is an attempt to move modified gogrep from the go-ruleguard project, so it can be used outside of the ruleguard as a library. Acknowl

Nov 9, 2022
🍫 A collection of common regular expressions for Go

CommonRegex A collection of often used regular expressions for Go Inspired by CommonRegex This is a collection of often used regular expressions. It p

Dec 31, 2022
A tool and library for using structural regular expressions.

Structural Regular Expressions sregx is a package and tool for using structural regular expressions as described by Rob Pike (link).

Dec 7, 2022
A command-line tool and library for generating regular expressions from user-provided test cases
A command-line tool and library for generating regular expressions from user-provided test cases

Table of Contents What does this tool do? Do I still need to learn to write regexes then? Current features How to install? 4.1 The command-line tool 4

Jan 9, 2023
Machine-readable regular expressions for identifying accession numbers for cultural heritage organizations in text.

Machine-readable regular expressions for identifying accession numbers for cultural heritage organizations in text.

Jun 14, 2022
Converts NFAs (and DFAs) to a regular expressions using the state removal method.

nfa-to-regex: convert NFAs (and DFAs) to regular expressions An implementation of the state removal technique for converting an NFA to a regular expre

Apr 29, 2022