Documentation
¶
Overview ¶
Package regex implements ECMAScript-style matching and string replacement in Go.
Compile prepares a pattern with explicit flags and Options matching limits. Regexp.MatchString checks input, Regexp.Replace applies replacement text, and Regexp.Replacer adapts replacement to a Data Masking field rule.
Semantics and limits ¶
This optional module implements compatibility behavior beyond Go's regexp package, including Unicode handling and JavaScript replacement tokens. The v flag is unsupported. Configure a timeout or backtracking limit for complex patterns. Global and sticky replacement can update lastIndex; Regexp serializes that state and is safe for concurrent use, but must not be copied.
Importing core Commons or Data Masking does not load this regex engine.
For usage and compatibility details, see the user guide.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Options ¶
Options configures matching limits. Zero values use the engine defaults: no timeout and a 100,000-entry backtracking stack. A negative stack size removes that bound; a negative timeout is invalid.
type Regexp ¶
type Regexp struct {
// contains filtered or unexported fields
}
Regexp is safe for concurrent use. Stateful replacements serialize access to lastIndex; stateless MatchString calls can run concurrently. Do not copy it.
func Compile ¶
Compile accepts ECMAScript d, g, i, m, s, u and y flags. The d flag has no effect on string replacement. Unicode set syntax (v) is not supported.
func (*Regexp) LastIndex ¶
LastIndex returns the UTF-16 offset used by sticky, non-global replacement.
func (*Regexp) MatchString ¶
MatchString searches from the beginning without reading or changing lastIndex. A sticky pattern must match at the beginning.
func (*Regexp) Replace ¶
Replace implements JavaScript string replacement tokens and global/sticky state. Lone UTF-16 surrogates are preserved as WTF-8 bytes in Go strings; encoding/json cannot preserve those bytes as JavaScript string code units.
Example ¶
package main
import (
"fmt"
"time"
"github.com/rambow-cloud/powertools-lambda-go/commons/regex"
)
func main() {
pattern, err := regex.Compile(`\d`, "g", regex.Options{MatchTimeout: time.Second})
if err != nil {
panic(err)
}
masked, err := pattern.Replace("order-123", "*")
fmt.Println(masked, err)
}
Output: order-*** <nil>
func (*Regexp) Replacer ¶
Replacer binds a replacement format for use with datamasking.Rule.Replace. The returned callback shares this Regexp's lastIndex.
func (*Regexp) SetLastIndex ¶
SetLastIndex sets an integer UTF-16 offset. Negative values are normalized to zero when used by replacement, as with JavaScript's ToLength operation.