Regex: allow full match

This commit is contained in:
andig 2022-04-05 07:54:23 +02:00
parent ef82e20df5
commit 9ecbbdd977
2 changed files with 27 additions and 1 deletions

View file

@ -181,7 +181,9 @@ func (p *Pipeline) Process(in []byte) ([]byte, error) {
if p.re != nil {
m := p.re.FindSubmatch(b)
if len(m) > 1 {
if len(m) == 1 {
b = m[0] // full match
} else if len(m) > 1 {
b = m[1] // first submatch
}
}

View file

@ -0,0 +1,24 @@
package pipeline
import (
"bytes"
"testing"
)
func TestRegex(t *testing.T) {
for _, re := range []string{`([0-9.]+)`, `[0-9.]+`} {
p, err := new(Pipeline).WithRegex(re)
if err != nil {
t.Error(err)
}
res, err := p.Process([]byte("12.3W"))
if err != nil {
t.Error(err)
}
if exp := []byte("12.3"); !bytes.Equal(res, exp) {
t.Errorf("Expected %s, got %s", exp, res)
}
}
}