Prometheus Regex

Test Prometheus label matching regex. Validate Alertmanager routes and PromQL selectors. Supports RE2 syntax.

Syntax Note

Prometheus uses RE2, which does not support backreferences or lookaround assertions.

Tester

RE2 Syntax
^ $
Prometheus regexes are fully anchored (must match entire string) by default in many contexts.
Note on RE2: Prometheus uses RE2, which does not support backreferences (e.g. \1) or lookaround assertions. This tester uses JS regex as an approximation but warns about lookarounds.

Testing RE2 Expressions

Prometheus and Alertmanager use the RE2 regular expression engine (from Google). It is designed to be linear time, meaning it deliberately avoids features like backreferences and lookaroound that can cause catastrophic backtracking.

Usage Guide

  1. Enter Pattern: Type your regex in the top field. Note that Prometheus selectors often implicitly anchor the pattern to the start ^ and end $. Our input field shows these anchors to remind you.
  2. Add Test Cases: Paste a list of metric names, label values, or log lines in the "TEST STRINGS" box.
  3. Check Matches: Matching lines will be highlighted in green instantly.

Common Patterns

  • .*: Match anything (greedy).
  • error|fail: Alternation (OR).
  • [a-z0-9]+: Alphanumeric characters.
  • node_.+: Metric names starting with "node_".

Examples of using Regex in the Prometheus ecosystem.

PromQL Selectors

Select time series based on regex matches (=~) or non-matches (!~):

# Select all metrics where 'job' starts with 'kube'
up{job=~"kube.*"}

# Select all requests with status string 4xx or 5xx
http_requests_total{status=~"4..|5.."}
Alertmanager Routing

Route alerts based on labels in alertmanager.yml:

route:
  receiver: 'default-receiver'
  routes:
    - match_re:
        service: ^(foo|bar)$
      receiver: 'service-owners'
Go (RE2)

Prometheus is written in Go. Here is how you use the same regex engine:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    // RE2 does not support lookarounds
    re := regexp.MustCompile(`^kube_.*_error$`)
    
    fmt.Println(re.MatchString("kube_pod_container_status_waiting_reason")) // false
    fmt.Println(re.MatchString("kube_pod_status_phase_error"))              // true
}

Place banner or text content here