Skip to content

Returns regexp matches on a channel using an io.Writer interface

Notifications You must be signed in to change notification settings

kgolding/regexer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Regexer

Simple go package to return regexp matches on a channel using an io.Writer interface.

  • Handles buffering of incoming data, and destruction of unmatched data
  • io.Writer interface
  • Returns [][]byte or []string using BytesToString helper

Usage

Create a regexer using a compiled regexp

r := NewRegexer(regexp.MustCompile(`(\w+)`))

Get results by reading the C channel, relying on the channel closing to exit the loop, and using the BytesToString helper to convert the results to string from the default []byte

go func() {
	for m := range r.C {
		t.Log(BytesToString(m))
	}
}()

Send data to the regxer using the io.Writer Write method

r.Write([]byte("This shouldn't match"))

Or for example from a file

f, err := os.Open("words.txt")
if err != nil {
	t.Fatal("Unable to open words.txt:", err)
}
defer f.Close()
_, err = io.Copy(r, f)

See the regexer_test.go file for full examples

About

Returns regexp matches on a channel using an io.Writer interface

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages