This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.
Your feedback helps us improve!
regexp operatorsโ
This page lists all operators available in the regexp sub-package of ro.
Installโ
First, import the sub-package in your project:
go get -u github.com/samber/ro/plugins/regepx
Findโ
Finds the first match of a regex pattern in byte slices.
import (
"regexp"
"github.com/samber/ro"
roregexp "github.com/samber/ro/plugins/regexp"
)
pattern := regexp.MustCompile(`\d+`)
obs := ro.Pipe[[]byte, []byte](
ro.Just([]byte("abc123def"), []byte("no numbers here")),
roregexp.Find[[]byte](pattern),
)
sub := obs.Subscribe(ro.PrintObserver[[]byte]())
defer sub.Unsubscribe()
// Next: [49 50 51]
// Next: []
// CompletedSimilar:Prototype:func Find[T ~[]byte](pattern *regexp.Regexp)FindAllโ
Finds all matches of a regex pattern in byte slices.
import (
"regexp"
"github.com/samber/ro"
roregexp "github.com/samber/ro/plugins/regexp"
)
pattern := regexp.MustCompile(`\d+`)
obs := ro.Pipe[[]byte, [][]byte](
ro.Just([]byte("abc123def456"), []byte("789ghi012")),
roregexp.FindAll[[]byte](pattern, -1), // -1 for unlimited matches
)
sub := obs.Subscribe(ro.PrintObserver[[][]byte]())
defer sub.Unsubscribe()
// Next: [[49 50 51] [52 53 54]]
// Next: [[55 56 57] [48 49 50]]
// CompletedSimilar:Prototype:func FindAll[T ~[]byte](pattern *regexp.Regexp, n int)FindAllStringโ
Finds all matches of a regex pattern in strings.
import (
"regexp"
"github.com/samber/ro"
roregexp "github.com/samber/ro/plugins/regexp"
)
pattern := regexp.MustCompile(`\d+`)
obs := ro.Pipe[string, []string](
ro.Just("abc123def456", "789ghi012"),
roregexp.FindAllString[string](pattern, -1), // -1 for unlimited matches
)
sub := obs.Subscribe(ro.PrintObserver[[]string]())
defer sub.Unsubscribe()
// Next: [123 456]
// Next: [789 012]
// CompletedSimilar:Prototype:func FindAllString[T ~string](pattern *regexp.Regexp, n int)FindAllSubmatchโ
Finds all submatches of a regex pattern in byte slices.
import (
"regexp"
"github.com/samber/ro"
roregexp "github.com/samber/ro/plugins/regexp"
)
pattern := regexp.MustCompile(`(\d+)-([a-z]+)`)
obs := ro.Pipe[[]byte, [][][]byte](
ro.Just([]byte("123-abc 456-def"), []byte("789-ghi")),
roregexp.FindAllSubmatch[[]byte](pattern, -1), // -1 for unlimited matches
)
sub := obs.Subscribe(ro.PrintObserver[[][][]byte]())
defer sub.Unsubscribe()
// Next: [[[49 50 51] [97 98 99]] [[52 53 54] [100 101 102]]]
// Next: [[[55 56 57] [103 104 105]]]
// CompletedPrototype:func FindAllSubmatch[T ~[]byte](pattern *regexp.Regexp, n int)FindAllStringSubmatchโ
Finds all submatches of a regex pattern in strings.
import (
"regexp"
"github.com/samber/ro"
roregexp "github.com/samber/ro/plugins/regexp"
)
pattern := regexp.MustCompile(`(\d+)-([a-z]+)`)
obs := ro.Pipe[string, [][]string](
ro.Just("123-abc 456-def", "789-ghi"),
roregexp.FindAllStringSubmatch[string](pattern, -1), // -1 for unlimited matches
)
sub := obs.Subscribe(ro.PrintObserver[[][]string]())
defer sub.Unsubscribe()
// Next: [[123-abc 123 abc] [456-def 456 def]]
// Next: [[789-ghi 789 ghi]]
// CompletedPrototype:func FindAllStringSubmatch[T ~string](pattern *regexp.Regexp, n int)FindStringโ
Finds the first match of a regex pattern in strings.
import (
"regexp"
"github.com/samber/ro"
roregexp "github.com/samber/ro/plugins/regexp"
)
pattern := regexp.MustCompile(`\d+`)
obs := ro.Pipe[string, string](
ro.Just("abc123def", "no numbers here"),
roregexp.FindString[string](pattern),
)
sub := obs.Subscribe(ro.PrintObserver[string]())
defer sub.Unsubscribe()
// Next: 123
// CompletedSimilar:Prototype:func FindString[T ~string](pattern *regexp.Regexp)FindSubmatchโ
Finds the first submatch of the pattern in the byte slice.
import (
"regexp"
"github.com/samber/ro"
roregexp "github.com/samber/ro/plugins/regexp"
)
pattern := regexp.MustCompile(`(\w+)\s+(\w+)`)
obs := ro.Pipe[[]byte, [][]byte](
ro.Just(
[]byte("hello world"),
[]byte("foo bar"),
[]byte("test"),
),
roregexp.FindSubmatch[[]byte](pattern),
)
sub := obs.Subscribe(ro.PrintObserver[[][]byte]())
defer sub.Unsubscribe()
// Next: [[104 101 108 108 111 32 119 111 114 108 100] [104 101 108 108 111] [119 111 114 108 100]]
// Next: [[102 111 111 32 98 97 114] [102 111 111] [98 97 114]]
// Next: []
// CompletedPrototype:func FindSubmatch[T ~[]byte](pattern *regexp.Regexp)FindStringSubmatchโ
Finds the first submatch of the pattern in the string.
import (
"regexp"
"github.com/samber/ro"
roregexp "github.com/samber/ro/plugins/regexp"
)
pattern := regexp.MustCompile(`(\w+)\s+(\w+)`)
obs := ro.Pipe[string, []string](
ro.Just("hello world", "foo bar", "test"),
roregexp.FindStringSubmatch[string](pattern),
)
sub := obs.Subscribe(ro.PrintObserver[[]string]())
defer sub.Unsubscribe()
// Next: [hello world hello world]
// Next: [foo bar foo bar]
// Next: []
// CompletedPrototype:func FindStringSubmatch[T ~string](pattern *regexp.Regexp)Matchโ
Checks if the pattern matches the byte slice.
import (
"regexp"
"github.com/samber/ro"
roregexp "github.com/samber/ro/plugins/regexp"
)
pattern := regexp.MustCompile(`hello`)
obs := ro.Pipe[[]byte, bool](
ro.Just(
[]byte("hello world"),
[]byte("goodbye world"),
[]byte("hello again"),
),
roregexp.Match[[]byte](pattern),
)
sub := obs.Subscribe(ro.PrintObserver[bool]())
defer sub.Unsubscribe()
// Next: true
// Next: false
// Next: true
// CompletedSimilar:Prototype:func Match[T ~[]byte](pattern *regexp.Regexp)MatchStringโ
Checks if the pattern matches the string.
import (
"regexp"
"github.com/samber/ro"
roregexp "github.com/samber/ro/plugins/regexp"
)
pattern := regexp.MustCompile(`hello`)
obs := ro.Pipe[string, bool](
ro.Just("hello world", "goodbye world", "hello again"),
roregexp.MatchString[string](pattern),
)
sub := obs.Subscribe(ro.PrintObserver[bool]())
defer sub.Unsubscribe()
// Next: true
// Next: false
// Next: true
// CompletedSimilar:Prototype:func MatchString[T ~string](pattern *regexp.Regexp)ReplaceAllโ
Replaces all matches in byte slice.
import (
"regexp"
"github.com/samber/ro"
roregexp "github.com/samber/ro/plugins/regexp"
)
pattern := regexp.MustCompile(`world`)
obs := ro.Pipe[[]byte, []byte](
ro.Just([]byte("hello world, goodbye world")),
roregexp.ReplaceAll[[]byte](pattern, []byte("universe")),
)
sub := obs.Subscribe(ro.PrintObserver[[]byte]())
defer sub.Unsubscribe()
// Next: [104 101 108 108 111 32 117 110 105 118 101 114 115 101 44 32 103 111 111 100 98 121 101 32 117 110 105 118 101 114 115 101]
// CompletedSimilar:Prototype:func ReplaceAll[T ~[]byte](pattern *regexp.Regexp, repl T)ReplaceAllStringโ
Replaces all matches of the pattern in the string with the replacement.
import (
"regexp"
"github.com/samber/ro"
roregexp "github.com/samber/ro/plugins/regexp"
)
pattern := regexp.MustCompile(`\bworld\b`)
obs := ro.Pipe[string, string](
ro.Just("hello world", "world peace", "new world order"),
roregexp.ReplaceAllString[string](pattern, "universe"),
)
sub := obs.Subscribe(ro.PrintObserver[string]())
defer sub.Unsubscribe()
// Next: hello universe
// Next: universe peace
// Next: new universe order
// CompletedSimilar:Prototype:func ReplaceAllString[T ~string](pattern *regexp.Regexp, repl T)FilterMatchโ
Filters byte slices that match pattern.
import (
"regexp"
"github.com/samber/ro"
roregexp "github.com/samber/ro/plugins/regexp"
)
pattern := regexp.MustCompile(`hello`)
obs := ro.Pipe[[]byte, []byte](
ro.Just(
[]byte("hello world"),
[]byte("goodbye world"),
[]byte("hello again"),
),
roregexp.FilterMatch[[]byte](pattern),
)
sub := obs.Subscribe(ro.PrintObserver[[]byte]())
defer sub.Unsubscribe()
// Next: [104 101 108 108 111 32 119 111 114 108 100]
// Next: [104 101 108 108 111 32 97 103 97 105 110]
// CompletedSimilar:Prototype:func FilterMatch[T ~[]byte](pattern *regexp.Regexp)FilterMatchStringโ
Filters strings that match pattern.
import (
"regexp"
"github.com/samber/ro"
roregexp "github.com/samber/ro/plugins/regexp"
)
pattern := regexp.MustCompile(`hello`)
obs := ro.Pipe[string, string](
ro.Just("hello world", "goodbye world", "hello again", "no match"),
roregexp.FilterMatchString[string](pattern),
)
sub := obs.Subscribe(ro.PrintObserver[string]())
defer sub.Unsubscribe()
// Next: hello world
// Next: hello again
// CompletedSimilar:Prototype:func FilterMatchString[T ~string](pattern *regexp.Regexp)