---
title: "Find an approximate substring near an offset in C#"
url: https://recursiveai.org/onediff/cookbook/find-a-fuzzy-substring-near-an-offset/
description: "Find the zero-based position of text that almost matches a pattern near an expected offset. Use OneDiff’s TryMatch without making or applying a patch."
version: "1.0.1"
---

# Find an approximate substring near an offset in C#

**OneDiff** is a free, open-source diff library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneDiff) · [GitHub](https://github.com/recursive-ai-oss/onediff-csharp) · [Docs](https://recursiveai.org/onediff/docs/) · [Download example (.zip)](https://recursiveai.org/onediff/cookbook/find-a-fuzzy-substring-near-an-offset/find-a-fuzzy-substring-near-an-offset.zip)

Use `TryMatch` to find text near a hint, even with typos. The default pattern limit is 32 UTF-16 code units.

```shell
dotnet add package RecursiveAI.OneDiff
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneDiff.Patching;

var matcher = new DmpPatcher();
var text = "Note: ship on Fryday.";
var pattern = "ship on Friday";
if (matcher.TryMatch(text, pattern, 5,
        out int offset))
    Console.WriteLine(offset);
else
    Console.WriteLine("No match");
```

Unzip [the example](https://recursiveai.org/onediff/cookbook/find-a-fuzzy-substring-near-an-offset/find-a-fuzzy-substring-near-an-offset.zip), then `dotnet run`.

Despite the class name, `TryMatch` doesn’t create or apply a patch.

Prints:

```text
6
```



The result is `6` even though the hint is `5` and `Fryday` differs from `Friday`. If the match isn’t good enough or the pattern is too long, `TryMatch` returns `false`; always check that result before using the offset, because `0` is also a valid match position.

## Variations

**Require a closer spelling match.** Replace the `matcher` declaration at the top. With this threshold, the example prints `No match`:

```csharp
var matcher = new DmpPatcher
    { MatchThreshold = 0.05 };
```

## Related recipes

- [Apply a diff-match-patch patch](https://recursiveai.org/onediff/cookbook/apply-diff-match-patch/)
- [Compare two strings](https://recursiveai.org/onediff/cookbook/compare-text/)

## The short answer

Call `DmpPatcher.TryMatch(text, pattern, expectedOffset, out offset)` to look for an approximate substring in C#. It returns the start of the match, not the matched text or its length. If all you need is an exact ordinal match, use [`string.IndexOf`](https://learn.microsoft.com/en-us/dotnet/api/system.string.indexof) instead.

## How does edit distance affect fuzzy substring matching?

An exact hit at the expected offset wins immediately. Otherwise, [OneDiff’s matcher](https://recursiveai.org/onediff/docs/patches/) scores candidates by adding the fraction of the pattern that needs edits to the distance from the hint divided by `MatchDistance`. A smaller score is better. The default `MatchThreshold` is `0.5` and the default `MatchDistance` is `1000`. Lower the threshold to reject weaker matches. Lower the distance to penalize nearby candidates more heavily relative to a candidate at the hint. If you have no position hint, pass `0` and increase `MatchDistance` for longer text: later matches still get a higher score because of their distance, and the matcher rejects them if that score exceeds `MatchThreshold`.

A short pattern can be sensitive to one typo: a single edit is a much larger share of four characters than of twenty. This is not just a whole-string edit-distance comparison. The matcher is looking for a starting position, so two equally similar pieces of text can score differently if one is farther from the hint.

## How do you get the index of an approximate match in C#?

The returned index counts UTF-16 code units, just like a C# string index. If there’s an emoji before the match, one visible character can take more than one index position. The hint is clamped between zero and the string length, so an out-of-range hint does not signal a failed search.

Set `MatchMaxBits` up to `64` if you need a longer pattern. For longer clues, pick a distinctive shorter piece and verify the surrounding text separately. Don’t assume the matched span has the same length as the pattern: an insertion or deletion can change its length, and `TryMatch` gives you only the start.
