Recursive AI

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.

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

Terminal
dotnet add package RecursiveAI.OneDiff

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
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, 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:

C#
var matcher = new DmpPatcher
    { MatchThreshold = 0.05 };

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 instead.

How does edit distance affect fuzzy substring matching?

An exact hit at the expected offset wins immediately. Otherwise, OneDiff’s matcher 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.

All onediff recipes