---
title: "Validate ONNX strings with RegexFullMatch in C#"
url: https://recursiveai.org/onemodel/cookbook/validate-onnx-string-input-with-regex-full-match/
description: "Run an opset 20 ONNX RegexFullMatch graph in C#. Pass a string tensor and read one Boolean result per element using a RE2-compatible pattern."
version: "1.5.0"
---

# Validate ONNX strings with RegexFullMatch in C#

**OneModel** is a free, open-source .NET library from Recursive AI for running ONNX models, including scikit-learn pipelines and XGBoost and LightGBM models. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneModel) · [GitHub](https://github.com/recursive-ai-oss/onemodel-csharp) · [Docs](https://recursiveai.org/onemodel/docs/) · [Download example (.zip)](https://recursiveai.org/onemodel/cookbook/validate-onnx-string-input-with-regex-full-match/validate-onnx-string-input-with-regex-full-match.zip)

`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](#how-codesonnx-was-made).

```shell
dotnet add package RecursiveAI.OneModel
```

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

```csharp
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](https://recursiveai.org/onemodel/cookbook/validate-onnx-string-input-with-regex-full-match/validate-onnx-string-input-with-regex-full-match.zip) 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:

```text
True, False
```

## The full program

```csharp
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:

```text
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:

```csharp
inputs.Add("codes",
    ["ab-123", " AB-123", "ZZ-000"]);
```

```text
False, False, True
```

To inspect your own export, replace `"codes.onnx"` with your model’s path. [OneModel’s pipeline guide](https://recursiveai.org/onemodel/docs/guide-sklearn-pipelines/#export) explains how to inspect input and output names and kinds. Use the listed names in your `Add` and output calls.

```csharp
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

- [Normalize and split text in ONNX with C#](https://recursiveai.org/onemodel/cookbook/normalize-and-split-strings-in-onnx/)
- [Use named inputs and outputs in an ONNX model](https://recursiveai.org/onemodel/cookbook/named-inputs-and-outputs/)

## 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](https://onnx.ai/onnx/operators/onnx__RegexFullMatch.html) 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.

```shell
python -m pip install onnx==1.23.2
python export.py
```

```python
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](https://github.com/google/re2/wiki/Syntax) rather than copying a .NET regular expression into the graph. OneModel’s [string operator documentation](https://recursiveai.org/onemodel/docs/ops-strings/#regular-expressions) 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.
