Complete reference for Code Search query syntax, including filters, boolean operators, regex, and structural search patterns.
A Sourcegraph query consists of two parts: a search pattern describing what to find, and filters that scope where to look. Both are optional—you can use filters alone to search commit history, file names, or repository names without a content pattern.
In keyword mode (the default since Sourcegraph 5.4), terms are matched independently anywhere in the file. Use quotes for phrases and /.../ for inline regex:
Syntax
Behavior
foo bar
Files containing both foo and bar anywhere
"foo bar"
Exact phrase match
/foo.*bar/
Regular expression match (RE2 syntax)
foo OR bar
Files containing either foo or bar
Matching is case-insensitive by default. Toggle the Aa button in the search bar or add case:yes to your query to enable case sensitivity.
Enable the (.*) toggle or include patternType:regexp to treat the entire query as a regular expression. Spaces between unquoted terms become .*? (fuzzy match). Quoted terms are matched literally.
Syntax
Behavior
foo bar
Matches foo(.*?)bar
"foo bar"
Matches the literal string foo bar
foo\nbar
Multiline match; \n is interpreted as a newline
foo\ bar or /foo bar/
Matches foo bar with a literal space
Sourcegraph uses RE2 syntax. Lookaheads, lookbehinds, and backreferences are not supported. For post-filtering with PCRE features, pipe results through src CLI and jq.
Structural search uses Comby patterns to match syntactic code structures. Use :[name] as a hole that matches any sequence of characters (including across lines):
fmt.Errorf(":[msg]", :[args])
if err := :[call]; err != nil { :[body] }
Structural search ignores whitespace differences, making it reliable across codebases with different formatting conventions. Enable it with the [] toggle or patternType:structural.
Structural search requires an indexed commit. It is not available for unindexed branches.
Filters narrow which repositories, files, or branches are searched. Multiple filters of the same type are intersected (AND logic) unless you use | within a single filter value for OR logic.
Aliases: language:, l:. Language detection is based on file extension by default. Enable the search-content-based-lang-detection feature flag for content-based detection.
These filters apply only when type:diff or type:commit is set:
Filter
Description
Example
author:pattern
Commits authored by a matching name or email
type:diff author:alice
-author:pattern
Exclude commits by matching author
type:commit -author:bot
after:"time"
Commits after the given time
after:"6 weeks ago"
before:"time"
Commits before the given time
before:"2024-01-01"
message:"string"
Commits whose message contains the string
type:commit message:"fix auth"
-message:"string"
Exclude commits with matching message
-message:"WIP"
Time values accept many formats: "yesterday", "5 days ago", "2 weeks ago", "november 1 2019", "2024-03-15", and Unix timestamps.Example—find all security-related changes on any branch in the last week:
Append a revision to the repo: filter with @, or use the separate rev: filter:
# Branch namerepo:^github\.com/myorg/api$@main# Commit hashrepo:^github\.com/myorg/api$@1735d48# Tagrepo:^github\.com/myorg/[email protected]# Multiple revisionsrepo:^github\.com/myorg/api$ rev:v1.0:v2.0:main# All branches (glob)repo:^github\.com/myorg/api$@*refs/heads/# All branches except mainrepo:^github\.com/myorg/api$@*refs/heads/:^refs/heads/main# Search at a point in time (Sourcegraph 5.4+)repo:^github\.com/myorg/api$ rev:at.time(1 year ago)