/dfa-regex-engine

simple DFA regex engine written in Go.

Primary LanguageGoMIT LicenseMIT

Go Report Card Follow on Twitter

DFA Regex Engine(in Golang)

This is simple DFA regex engine written in Go.

Description

This engine may not be practical as I just wrote this to learn how the dfa engine works.

Metacharacters

The engine supports the following metacharacters.

Metacharacter Desciption Examples
* Matches 0 or more repetitions of a pattern. a* = a, aaa...
+ Matches 1 or more repetitions of a pattern. (abc)+ = abc, abcabc, abcabcabc...
| Match any of the left and right patterns.(like the Boolean OR) a|b|c = a, b, c

Usage

re := dfaregex.Compile("(a|b)c*")
re.Match("acccc")   // => true

Example

package main

import (
	"fmt"
	"github.com/8ayac/dfa-regex-engine/dfaregex"
)

func main() {
	regex := "piyo(o*)"
	re := dfaregex.Compile(regex)

	for _, s := range []string{"piyo", "piyoooo", "piy0"} {
		if re.Match(s) {
			fmt.Printf("%s\t=> matched.\n", s)
		} else {
			fmt.Printf("%s\t=> NOT matched.\n", s)
		}
	}
}
$ go run main.go
piyo	=> matched.
piyoooo	=> matched.
piy0	=> NOT matched.

How to install

$ go get -u github.com/8ayac/dfa-regex-engine

License

MIT

Author

8ayac