← Library String Deterministic

RegexMatchOp

Tests whether a string matches a Go regular expression. The pattern is compiled once at setup time.

Inputs

Field Type Description
Input*stringString to test

Outputs

Field Type Description
MatchboolTrue if the input matches the pattern

Params

Key Type Default Description
patternstringrequiredGo regular expression, compiled at setup

Typical Use Cases

  • Format validation (email, phone, UUID)
  • Keyword presence detection via regex
  • Routing based on text pattern

Pattern compiled at setup. Invalid patterns cause a setup-time error, not a runtime error. Test your regex before deploying.

Complete Runnable Example

Validates that a user-supplied phone number matches the expected format before routing to the SMS step.

main.go
package main

import (
	"context"
	"encoding/json"
	"log"
	"os"
	"time"

	"github.com/akennis/sparsi-go/library"
	_ "github.com/akennis/dagor/operator/builtin"

	"github.com/panjf2000/ants/v2"
	"github.com/akennis/dagor"
	"github.com/akennis/dagor/graph"
)

func main() {
	phone := "+1-800-555-0199"
	library.RegisterConst("phone_val", phone)

	g, err := graph.NewBuilder("regexmatch_demo").
		Vertex("src").Op("phone_val").Output("Result", "wire_in").
		Vertex("mx").Op("RegexMatchOp").
		Params(map[string]string{"pattern": `^\+?[0-9\-]{7,15}$`}).
		Input("Input", "wire_in").
		Output("Match", "is_valid").
		Build()
	if err != nil {
		log.Fatalf("build: %v", err)
	}

	pool, err := ants.NewPool(4)
	if err != nil {
		log.Fatalf("pool: %v", err)
	}
	defer pool.Release()

	eng, err := dagor.NewEngine(g, pool)
	if err != nil {
		log.Fatalf("engine: %v", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
	defer cancel()

	if err := eng.Run(ctx); err != nil {
		log.Fatalf("run: %v", err)
	}

	out := map[string]any{"phone": phone}
	if raw, ok := eng.GetOutput("is_valid"); ok {
		out["is_valid_phone"] = *raw.(*bool)
	}
	enc := json.NewEncoder(os.Stdout)
	enc.SetIndent("", "  ")
	enc.Encode(out)
}