Skip to content

Regex & AST Patterns

Beyond plain-text and symbol search, Reflex supports two advanced query modes: regex for pattern matching and AST queries for structural code search.

Use --regex (or -r) to search with regular expressions:

Terminal window
rfx query "handle[A-Z]\w+" --regex

Reflex optimizes regex queries by extracting literal substrings from the pattern, using them for trigram narrowing, then applying the full regex only to candidate files. This keeps most regex searches fast.

Terminal window
# Functions starting with "get" or "set"
rfx query "(?:get|set)[A-Z]\w+" --regex --symbols
# TODO comments with assignees
rfx query "TODO\(\w+\):" --regex
# IP address patterns
rfx query "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" --regex

Regex searches that contain literal substrings of 3+ characters are fast — Reflex extracts trigrams from the literals to narrow the search space.

Patterns with few or no literals (like .* or [a-z]+) may fall back to scanning all files, which is slower on large codebases. Use --timeout to cap execution time:

Terminal window
rfx query "." --regex --timeout 5

AST queries use Tree-sitter’s S-expression syntax to search for structural patterns in parsed code. This finds code by its syntactic structure rather than its text content.

Terminal window
rfx query "(function_declaration name: (identifier) @name)" --regex --lang rust
  • Finding functions with specific parameter patterns
  • Matching nested structural patterns (e.g., a loop inside a conditional)
  • Searching for code shapes that aren’t captured by symbol extraction

For 95% of use cases, --symbols with --kind is the right choice. It’s faster and covers the most common needs: finding definitions by name and type.

Terminal window
# Prefer this (fast):
rfx query "validate" --symbols --kind function
# Over this (slow):
rfx query "(function_declaration name: (identifier) @name (#match? @name \"validate\"))" --regex

You can combine regex with symbol filtering:

Terminal window
# Regex + symbols: find function definitions matching a pattern
rfx query "handle[A-Z]" --regex --symbols --kind function