Compile Go regular expressions to Go code

Overview

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
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

Konstantin 32 Jul 11, 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

GopherJS 11.3k Aug 12, 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

Google 10.5k Aug 12, 2022
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 {

Eli Yukelzon 101 Aug 9, 2022
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

Iskander (Alex) Sharipov 30 Jun 27, 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

MinJae Kwon 823 Aug 12, 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).

Zachary Yedidia 37 Jul 22, 2022
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

Peter M. Stahl 5.5k Aug 9, 2022
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.

San Francisco International Airport Museum 6 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

David Wolever 3 Apr 29, 2022
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

David Wolever 3 Apr 29, 2022
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

Robert Fratto 10 Aug 7, 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

Steve J Donovan 8 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

Rob Pike 79 Aug 7, 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

Luca Sepe 17 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

Canonical 1 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

Daniel G. Taylor 7 Aug 17, 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

Ozzo Framework 441 Aug 8, 2022
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

Ahmy Yulrizka 14 Dec 21, 2020
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

Victor 31 Dec 30, 2021
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

Ned McClain 10 Apr 18, 2022
This image is primarily used to ping/call a URL on regular intervals using Kubernetes (k8s) CronJob.

A simple webhook caller This image is primarily used to ping/call a URL on regular intervals using Kubernetes (k8s) CronJob. A sample job would look s

Goavega Software 0 Nov 30, 2021
run regular Docker images in KVM/Qemu

runq runq is a hypervisor-based Docker runtime based on runc to run regular Docker images as a lightweight KVM/Qemu virtual machine. The focus is on s

null 723 Aug 8, 2022
It's so many regular expression forms are difficult to understand, such as perl, python, grep awk

Introduction Jamie Zawinski: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. It

aceking 0 Mar 31, 2022
Graceful shutdown with repeating "cron" jobs (running at a regular interval) in Go

Graceful shutdown with repeating "cron" jobs (running at a regular interval) in Go Illustrates how to implement the following in Go: run functions ("j

Valentin Padurean (Ogg) 1 May 30, 2022
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

null 248 Jul 6, 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

Sergey Treinis 22 Jul 4, 2022
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

null 248 Jul 6, 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

Mitchell Hashimoto 4.3k Aug 11, 2022