This guide provides practical, ready-to-run examples for using SharpCodeSearch.
Build the backend first:
cd src/backend
dotnet buildPowerShell (Windows):
- Use single quotes for patterns:
'Console.WriteLine($arg$)' - Double quotes cause variable expansion and will break patterns
Bash/Zsh (Linux/Mac):
- Use single or double quotes:
"Console.WriteLine($arg$)" - Single quotes recommended for consistency
Examples below use PowerShell syntax (suitable for Linux/Mac with minor adjustments)
dotnet run -- --pattern 'Console.WriteLine($arg$)' --file YourFile.csusing System;
class Program
{
static void Main()
{
Console.WriteLine("Hello World");
Console.WriteLine("Debug info");
var message = "Test";
Console.WriteLine(message);
}
}Finds all 3 Console.WriteLine calls and extracts the arguments:
Found 3 match(es):
test.cs:7:9
Console.WriteLine("Hello World")
Placeholders:
arg = "Hello World"
test.cs:8:9
Console.WriteLine("Debug info")
Placeholders:
arg = "Debug info"
test.cs:10:9
Console.WriteLine(message)
Placeholders:
arg = message
Note: Both Console.WriteLine($arg$) and WriteLine($arg$) work.
dotnet run -- --pattern '$x$ + $y$' --file Calculator.cs --output textclass Calculator
{
int Add(int a, int b)
{
return a + b;
}
void Process()
{
int sum = 5 + 10;
int total = sum + 15;
}
}Finds both addition operations and captures left/right operands:
- Match 1:
a + b(left=a, right=b) - Match 2:
5 + 10(left=5, right=10) - Match 3:
sum + 15(left=sum, right=15)
dotnet run -- --pattern '$obj$.ToString()' --file DataModel.csclass DataModel
{
void PrintData()
{
var id = 123;
var name = "User";
Console.WriteLine(id.ToString());
Console.WriteLine(name.ToString());
}
}Finds:
id.ToString()(obj=id)name.ToString()(obj=name)
dotnet run -- --pattern 'var $name$ = $value$;' --file Variables.csclass Variables
{
void Method()
{
var count = 10;
var message = "Hello";
var result = Calculate();
}
}Finds all var declarations and extracts variable names and initializers.
dotnet run -- --pattern '$var:regex=temp.*$ = $value$' --file Config.csclass Config
{
void LoadSettings()
{
var temporaryFile = "temp.txt";
var tempDirectory = "/tmp";
var temperature = 25;
var config = "app.config"; // Won't match
}
}Finds only variables starting with "temp":
temporaryFiletempDirectorytemperature
dotnet run -- --pattern 'Console.WriteLine($msg$)' --file App.cs{
"matchCount": 2,
"matches": [
{
"filePath": "App.cs",
"line": 5,
"column": 9,
"matchedCode": "Console.WriteLine(\"Hello\")",
"placeholders": {
"msg": "\"Hello\""
}
},
{
"filePath": "App.cs",
"line": 6,
"column": 9,
"matchedCode": "Console.WriteLine(\"World\")",
"placeholders": {
"msg": "\"World\""
}
}
]
}Note: Argument placeholders capture the arguments without parentheses.
dotnet run -- --pattern '$a$ == $b$' --file Comparisons.cs --output textclass Comparisons
{
bool Compare(int x, int y)
{
if (x == y) return true;
return x == 0 || y == 0;
}
}Finds:
x == yx == 0y == 0
dotnet run -- --pattern 'string.Format($args$)' --file StringOps.csclass StringOps
{
void FormatStrings()
{
var s1 = string.Format("Hello {0}", name);
var s2 = string.Format("Value: {0}, {1}", x, y);
var s3 = $"Interpolated: {name}"; // Won't match
}
}Finds the two string.Format calls:
- Match 1:
args = "Hello {0}", name - Match 2:
args = "Value: {0}, {1}", x, y
Note: Multiple arguments are captured as comma-separated values without the surrounding parentheses.
dotnet run -- --pattern '$obj$.Length' --file Arrays.csclass Arrays
{
void ProcessArrays()
{
var arr = new int[] { 1, 2, 3 };
var count = arr.Length;
var str = "test";
var len = str.Length;
}
}Finds:
arr.Lengthstr.Length
dotnet run -- --pattern 'if ($condition$)' --file Logic.csclass Logic
{
void CheckConditions(int x)
{
if (x > 0)
DoSomething();
if (x < 100)
{
DoOther();
}
}
}Finds both if statements and extracts conditions:
x > 0x < 100
Create this PowerShell script to test all examples:
# test-patterns.ps1
# Create test file
$testCode = @"
using System;
class TestProgram
{
static void Main()
{
Console.WriteLine("Hello");
Console.WriteLine("World");
int x = 5 + 10;
var name = "Test";
name.ToString();
}
}
"@
Set-Content "TestFile.cs" $testCode
# Test 1: Find Console.WriteLine
Write-Host "`n=== Test 1: Console.WriteLine ===" -ForegroundColor Cyan
dotnet run --project src/backend -- --pattern 'Console.WriteLine($arg$)' --file TestFile.cs --output text
# Test 2: Find additions
Write-Host "`n=== Test 2: Additions ===" -ForegroundColor Cyan
dotnet run --project src/backend -- --pattern '$x$ + $y$' --file TestFile.cs --output text
# Test 3: Find ToString calls
Write-Host "`n=== Test 3: ToString calls ===" -ForegroundColor Cyan
dotnet run --project src/backend -- --pattern '$obj$.ToString()' --file TestFile.cs --output text
# Cleanup
Remove-Item "TestFile.cs"Run it:
pwsh test-patterns.ps1- Open Command Palette:
Ctrl+Shift+P(orCmd+Shift+Pon Mac) - Type:
Sharp Code Search: Search - Enter your pattern:
Console.WriteLine($arg$) - View results in the sidebar
- Start simple, add complexity gradually
- Use descriptive placeholder names
- Test patterns on small files first
- Search single files first before workspace-wide
- Use constraints to narrow results
- Patterns are case-sensitive
- Use
--output textfor human-readable output - Use
--output jsonfor machine processing - Check placeholder capture with simple patterns first
# Find all LINQ queries
dotnet run -- --pattern '$collection$.Where($predicate$)' --file Data.cs
# Find null checks
dotnet run -- --pattern 'if ($var$ == null)' --file Safety.cs
# Find string concatenation
dotnet run -- --pattern '$str1$ + $str2$' --file Strings.cs
# Find array access
dotnet run -- --pattern '$array$[$index$]' --file Arrays.cs
# Find type casts
dotnet run -- --pattern '($type$)$expr$' --file Casts.cs- Read QUICK_START_AND_EXAMPLES.md for advanced patterns
- See ARCHITECTURE.md for technical details
- Check ROADMAP.md for upcoming features
Happy Pattern Matching! 🎯