regex

package module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 11 Imported by: 1

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

type Options struct {
	MatchTimeout             time.Duration
	MaxBacktrackingStackSize int
}

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

func Compile(pattern, flags string, limits Options) (*Regexp, error)

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

func (r *Regexp) LastIndex() int

LastIndex returns the UTF-16 offset used by sticky, non-global replacement.

func (*Regexp) MatchString

func (r *Regexp) MatchString(input string) (bool, error)

MatchString searches from the beginning without reading or changing lastIndex. A sticky pattern must match at the beginning.

func (*Regexp) Replace

func (r *Regexp) Replace(input, format string) (string, error)

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

func (r *Regexp) Replacer(format string) func(string) (string, error)

Replacer binds a replacement format for use with datamasking.Rule.Replace. The returned callback shares this Regexp's lastIndex.

func (*Regexp) SetLastIndex

func (r *Regexp) SetLastIndex(index int)

SetLastIndex sets an integer UTF-16 offset. Negative values are normalized to zero when used by replacement, as with JavaScript's ToLength operation.

func (*Regexp) String

func (r *Regexp) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL