Skip to content

Commit

Permalink
Regex: allow full match
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Apr 5, 2022
1 parent ef82e20 commit 9ecbbdd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion provider/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
24 changes: 24 additions & 0 deletions provider/pipeline/pipeline_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 9ecbbdd

Please sign in to comment.