← Library String Deterministic

RegexExtractOp

Extracts the first match of a Go regex from a string. If a capturing group is present, returns group 1. Returns empty string on no match.

Inputs

Field Type Description
Input*stringString to search

Outputs

Field Type Description
ResultstringFirst match (or group 1 if capturing group present); empty on no match

Params

Key Type Default Description
patternstringrequiredGo regular expression, compiled at setup

Typical Use Cases

  • Extracting email addresses from prose
  • Pulling URLs or codes from text
  • Extracting structured fragments with a capture group

Complete Runnable Example

Extracts the numeric order ID from a customer notification string using a capturing group.

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() {
	text := "Your order ORD-48291 has been shipped."
	library.RegisterConst("text_val", text)

	g, err := graph.NewBuilder("regexextract_demo").
		Vertex("src").Op("text_val").Output("Result", "wire_in").
		Vertex("ex").Op("RegexExtractOp").
		Params(map[string]string{"pattern": "ORD-([0-9]+)"}).
		Input("Input", "wire_in").
		Output("Result", "order_id").
		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{"text": text}
	if raw, ok := eng.GetOutput("order_id"); ok {
		out["order_id"] = *raw.(*string)
	}
	enc := json.NewEncoder(os.Stdout)
	enc.SetIndent("", "  ")
	enc.Encode(out)
}