---
title: "Extract contact records from plain text in C#"
url: https://recursiveai.org/onecontact/cookbook/extract-contact-records-from-plain-text/
description: "Extract contact records from plain text in C# with a runnable OneContact example for .NET 10, actual output and explicit input handling."
version: "1.0.0"
---

# Extract contact records from plain text in C#

**OneContact** is a free, open-source contact data library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies, no network access.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneContact.Extraction) · [GitHub](https://github.com/recursive-ai-oss/onecontact-csharp) · [Docs](https://recursiveai.org/onecontact/docs/) · [Download example (.zip)](https://recursiveai.org/onecontact/cookbook/extract-contact-records-from-plain-text/extract-contact-records-from-plain-text.zip)

Extract grouped contacts and ungrouped fields from a text signature.

```shell
dotnet add package RecursiveAI.OneContact.Extraction
```

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

```csharp
using RecursiveAI.OneContact
    .Extraction;

var result = new ContactExtractor()
    .ExtractText(
    "Name: Ada Example\nada@example.com");
foreach (var contact in result.Records)
    Console.WriteLine(contact.Name?.Text);
```

Unzip [the example](https://recursiveai.org/onecontact/cookbook/extract-contact-records-from-plain-text/extract-contact-records-from-plain-text.zip), then `dotnet run`.

Prints:

```text
Ada Example
```

## The full program

The full program supplies a US region for the national address. All names, businesses and contact details below are fictional. It reports the grouped fields, the complete flat-field count, the ungrouped email and extraction issues.

Its entire input is:

```text
Name: Ada Example
Company: Example Works LLC
Phone: +1 202-555-0123
Email: ada@example.com
Address: 123 Main St, Springfield, IL 62704

support@example.com
```

```csharp
using RecursiveAI.OneContact;
using RecursiveAI.OneContact.Extraction;

string text = """
Name: Ada Example
Company: Example Works LLC
Phone: +1 202-555-0123
Email: ada@example.com
Address: 123 Main St, Springfield, IL 62704

support@example.com
""";
var extractor = new ContactExtractor { DefaultRegion = Region.US };
var result = extractor.ExtractText(text);
foreach (var record in result.Records)
{
    Console.WriteLine($"Record: {record.Kind} {record.Name?.Text}");
    foreach (var field in record.Fields)
        Console.WriteLine($"  {field.Kind}: {field.Text}");
}
Console.WriteLine($"Flat fields: {result.Fields.Count}");
foreach (var field in result.Ungrouped)
    Console.WriteLine($"Ungrouped {field.Kind}: {field.Text}");
Console.WriteLine($"Issues: {result.Issues.Count}");
```

`dotnet run` prints:

```text
Record: Person Ada Example
  Name: Ada Example
  Organization: Example Works LLC
  Phone: +1 202-555-0123
  Email: ada@example.com
  Address: 123 Main St, Springfield, IL 62704
Flat fields: 6
Ungrouped Email: support@example.com
Issues: 0
```

Inputs beyond the configured extraction input limits throw `ContactExtractionException`; ungrouped or unnormalized fields remain visible in the result rather than being invented.

## Variations

### Keep flat fields without assuming a person

Run this on its own to collect details even when no name establishes a record:

```csharp
using RecursiveAI.OneContact
    .Extraction;

var result = new ContactExtractor()
    .ExtractText(
    "support@example.com");
foreach (var field in result.Fields)
    Console.WriteLine($"{field.Kind}: {field.Text}");
Console.WriteLine($"Ungrouped: {result.Ungrouped.Count}");
```

```text
Email: support@example.com
Ungrouped: 1
```

## Related recipes

- [Extract contacts from HTML](https://recursiveai.org/onecontact/cookbook/extract-contacts-from-html/)
- [Extract emails from text](https://recursiveai.org/onecontact/cookbook/extract-emails-from-text/)

## The short answer

Use an instance of `ContactExtractor` from `RecursiveAI.OneContact.Extraction` and call `ExtractText`, not the static HTML helper. The result contains grouped person or organization records, all fields in input order, ungrouped details and issues. Set `DefaultRegion` when your source uses national phone numbers or addresses that need country context.

OneContact 1.0.0 runs extraction with rules, not a trained contact-span model. It reads the text you pass in; it does not visit a website or search for more information about the people it finds. That makes the result a reading of your input, not an independently verified contact list.

## How do I extract contact records from plain text in C#?

Keep the source as text and call `ExtractText` explicitly. A string is not inspected to guess whether it contains HTML. The [API shapes documentation](https://recursiveai.org/onecontact/docs/api-shapes/) separates the format-specific instance methods so the caller decides which reader to use.

The full program shows a signature with a name, employer, phone, email and postal address. A `Name:` label provides evidence that the words following it name a person. The company is retained as that person’s organization field. The separate support email remains ungrouped: an email address alone does not establish who owns it.

Do not discard `Fields` just because `Records` is empty. The flat list is useful for importing contact details for review; `Ungrouped` identifies details that the grouping rules did not attach to a record. Keep `Issues` alongside the values when an import needs a review trail rather than silently accepting every field.

## How do I extract contact records from plain text as JSON?

Choose what your application needs to persist before serializing. A grouped contact and a flat email field are different kinds of result. An export that contains only `Records` can leave out the support address in this example. An export intended for review should retain both grouped and ungrouped details.

The [extraction result model](https://recursiveai.org/onecontact/docs/extraction/) gives each field a kind, raw source slice, text, span, source and optional normalized value. Preserve these distinctions in your JSON schema. `Text` is readable extracted content; `Normalized` is the core parser’s interpretation when one is available. A field without a normalized value is not evidence that the original source had no contact information.

Offsets from `ExtractText` are UTF-16 offsets into the exact input string, including line endings. If you replace line endings or trim the input before showing the stored offsets, they no longer point into that changed string. Retain the original input when you need to highlight a source span later.

## Can I extract contact details from HTML the same way?

Use `ExtractHtml` for HTML, rather than stripping tags yourself and assuming the offsets still match the original page. The HTML path reads supported structured markup and visible text. The plain-text path treats lines as its unit and blank lines or separator lines as paragraph boundaries.

The distinction affects evidence as well as offsets. Plain-text notes can use cues such as greetings and header labels that would be ambiguous in web navigation or bylines. Capitalized words alone do not prove that a contact name is present. The [documented text rules](https://recursiveai.org/onecontact/docs/extraction/) explain which nearby cues establish a name.

Country context is separate from these cues. The region is not guessed. Without one, an address can remain unnormalized with `address.country-required`, and an unlabelled national phone can be dropped. Use the region of the source only when you actually know it; a default is context supplied by your application, not a country detected by the extractor.
