iterex

iter
···re·
ex
Iterator-based regular expressions for Go.
TL;DR
- Change
regexp
to iterex
- Change
All
to Each
- Iterate!
This package is like the standard regexp
with two differences:
- it returns iterators (instead of all matches at once)
- limits are optional (instead of having to specify
-1
)
Requirements
Go 1.23 or higher - the range
keyword accepts iterator functions sice that version:
Documentation
Detailed documentation:
Quick start
Install
go get github.com/rsp/iterex
Import
import "github.com/rsp/iterex"`
Use
Instead of:
re := regexp.MustCompile(pattern)
you call:
ir := iterex.MustCompile(pattern)
And instead of:
slice := re.FindAllString(str, -1)
you call:
iterator := re.FindEachString(str)
(limit is optional, defaults to -1
meaning no limit)
Then you can iterate:
for s := range iterator {
fmt.Println(s)
}
Introduction
iterex
provides lazy version of the "All" receiver functions from
standard regexp
package.
Instead of "All" they have "Each" in their names because instead of
returing all results at once, they return iterators that iterate over each result,
one at a time.
You can terminate the iteration early by break
ing from the loop.
Constructors
This package provides 4 ways to compile a pattern,
just like the standard regexp
package:
- Return error:
ir, err := iterex.Compile("...")
ir, err := iterex.CompilePOSIX("...")
- Panic on error:
ir := iterex.MustCompile("...")
ir := iterex.MustCompilePOSIX("...")
Receiver functions
For every regexp
receiver function with All
in the name,
it provides a function with Each
that returns an iterator instead of all results at once.
Byte slice functions
var b []byte
ir.FindEach(b, n)
- iterator version of re.FindAll(b, n)
ir.FindEachIndex(b, n)
- iterator version of re.FindAllIndex(b, n)
ir.FindEachSubmatch(b, n)
- iterator version of re.FindAllSubmatch(b, n)
ir.FindEachSubmatchIndex(b, n)
- iterator version of re.FindAllSubmatchIndex(b, n)
Note that unlike in regexp
the n
is optional:
ir.FindEach(b)
is the same as ir.FindEach(b, -1)
(no limit)
String functions
var s string
ir.FindEachString(s, n)
- iterator version of re.FindAllString(s, n)
ir.FindEachStringIndex(s, n)
- iterator version of re.FindAllStringIndex(s, n)
ir.FindEachStringSubmatch(s, n)
- iterator version of re.FindAllStringSubmatch(s, n)
ir.FindEachStringSubmatchIndex(s, n)
- iterator version of re.FindAllStringSubmatchIndex(s, n)
Note that unlike in regexp
the n
is optional:
ir.FindEachString(s)
is the same as ir.FindEachString(s, -1)
(no limit)
Author
Rafał Pocztarski -
rsp
License
MIT License (Expat). See LICENSE.md for details.