codes.onnx (in the zip) matches product codes like AB-123. It uses ONNX opset 20. The pattern is fixed when the graph is built, not passed from C#; change it in export.py. See how it was made.
dotnet add package RecursiveAI.OneModelRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneModel; using var model = Model.Load("codes.onnx"); using var inputs = new ModelInputs(); inputs.Add("codes", ["AB-123", "AB-1234"]); using var result = model.Run(inputs); var matches = result.Values<bool>("matches"); Console.WriteLine(string.Join(", ", matches));
The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.
If you paste the snippet, copy codes.onnx from the zip next to Program.cs.
Prints:
True, FalseThe full program
using RecursiveAI.OneModel; using var model = Model.Load("codes.onnx"); using var inputs = new ModelInputs(); string[] codes = ["AB-123", "AB-1234"]; inputs.Add("codes", codes); using var result = model.Run(inputs); var matches = result.Values<bool>("matches"); for (int i = 0; i < codes.Length; i++) Console.WriteLine( $"{codes[i]}: {matches[i]}");
dotnet run prints:
AB-123: True AB-1234: False
AB-123 matches. AB-1234 doesn’t: a matching prefix isn’t enough. A nonmatch returns false; an unsupported pattern, such as one with a backreference, throws SessionException when the session is created.
Variations
Replace the inputs.Add line to check that case and spaces matter for this pattern:
inputs.Add("codes", ["ab-123", " AB-123", "ZZ-000"]);
False, False, TrueTo inspect your own export, replace "codes.onnx" with your model’s path. OneModel’s pipeline guide explains how to inspect input and output names and kinds. Use the listed names in your Add and output calls.
using RecursiveAI.OneModel; using var model = Model.Load("codes.onnx"); foreach (var input in model.Session.Inputs) Console.WriteLine( $"In: {input.Name} ({input.Kind})"); foreach (var output in model.Session.Outputs) Console.WriteLine( $"Out: {output.Name} ({output.Kind})");
Related recipes
The short answer
Use the ONNX RegexFullMatch operator with a RE2-compatible pattern attribute and opset 20 or later. In C#, add the input with ModelInputs.Add and read the result with Values<bool>. Each output Boolean corresponds to the input string in the same position. The pattern belongs to the graph; it isn’t a second input tensor.
Does RegexFullMatch check the whole string or find a substring?
It checks the whole string, as the ONNX operator specification describes. With [A-Z]{2}-[0-9]{3}, input such as AB-1234 or AB-123 doesn’t pass just because it contains AB-123. You don’t need to add ^ and $ to enforce a whole-string match. There’s no trimming or case conversion in this graph: [A-Z] asks for ASCII uppercase letters, and [0-9] asks for ASCII digits. The result checks format only; it doesn’t tell you whether the code exists in a catalog.
How codes.onnx was made
The script builds one node with the pattern stored in the model. Save it as export.py or use the copy in the zip. To rebuild codes.onnx, run these commands in a Python environment from the folder containing the script. The C# program uses the supplied model and doesn’t need Python.
python -m pip install onnx==1.23.2 python export.py
import onnx from onnx import TensorProto as T from onnx import helper as h node = h.make_node( "RegexFullMatch", ["codes"], ["matches"], pattern=r"[A-Z]{2}-[0-9]{3}", ) value_info = h.make_tensor_value_info codes = value_info("codes", T.STRING, ["N"]) matches = value_info( "matches", T.BOOL, ["N"], ) graph = h.make_graph( [node], "check_codes", [codes], [matches], ) model = h.make_model( graph, opset_imports=[h.make_opsetid("", 20)], ir_version=9, ) onnx.checker.check_model(model) onnx.save(model, "codes.onnx")
Does RE2 support lookahead or backreferences?
No. Start with the RE2 syntax reference rather than copying a .NET regular expression into the graph. OneModel’s string operator documentation lists patterns it refuses, including lookahead, lookbehind, backreferences, and named Unicode scripts such as \p{Greek}. Shorthand classes such as \d and \w use ASCII rules. An unsupported expression is a model setup error, not a Boolean false for every row.