← Library Predicate Deterministic

IfStringRegexMatchOp

Returns true if the input matches the configured Go regular expression.

Inputs

FieldTypeDescription
Input*stringString to test

Outputs

FieldTypeDescription
MatchboolTrue when input matches pattern

Params

KeyTypeDefaultDescription
patternstringrequiredGo regular expression, compiled at setup

Typical Use Cases

  • Pattern-based conditional routing
  • Format validation gates
  • Complex string condition without an extra op

Pattern compiled at setup. Invalid patterns cause a setup error, not a runtime error.

Complete Runnable Example

Validates that an email address matches a standard format before passing it to a send 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() {
	email := "alice@example.com"
	library.RegisterConst("email_val", email)

	g, err := graph.NewBuilder("regex_demo").
		Vertex("src").Op("email_val").Output("Result", "email_wire").
		Vertex("check").Op("IfStringRegexMatchOp").
		Params(map[string]string{
			"pattern": `^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$`,
		}).
		Input("Input", "email_wire").
		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{"email": email}
	if raw, ok := eng.GetOutput("is_valid"); ok {
		out["is_valid_email"] = *raw.(*bool)
	}
	enc := json.NewEncoder(os.Stdout)
	enc.SetIndent("", "  ")
	enc.Encode(out)
}