---
title: "Find IPv4 and IPv6 addresses in text in C#"
url: https://recursiveai.org/onenlp/cookbook/extract-ip-addresses-from-text/
description: "Extract IPv4 and IPv6 addresses from a C# string with OneNLP. Get the original match and its UTF-16 start and end offsets for slicing or highlighting."
version: "1.0.0"
---

# Find IPv4 and IPv6 addresses in text in C#

**OneNLP** is a free, open-source natural language processing library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneNlp) · [GitHub](https://github.com/recursive-ai-oss/onenlp-csharp) · [Docs](https://recursiveai.org/onenlp/docs/) · [Download example (.zip)](https://recursiveai.org/onenlp/cookbook/extract-ip-addresses-from-text/extract-ip-addresses-from-text.zip)

```shell
dotnet add package RecursiveAI.OneNlp
```

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

```csharp
using RecursiveAI.OneNlp;

string text = "📍 192.0.2.1, 2001:0DB8::1.";
foreach (var hit in Nlp.Sequences(text))
    if (hit.Label == "IP_ADDRESS")
        Console.WriteLine($"{hit.Start}.." +
            $"{hit.End}: {hit.Text}");
```

```text
3..12: 192.0.2.1
14..26: 2001:0DB8::1
```

`End` is exclusive, so `text[hit.Start..hit.End]` gives the original spelling. The pin emoji takes two UTF-16 code units and the space takes one, so the first address starts at index `3`. If the text has no `IP_ADDRESS` matches, the loop prints nothing.

Unzip [the example](https://recursiveai.org/onenlp/cookbook/extract-ip-addresses-from-text/extract-ip-addresses-from-text.zip), then `dotnet run`.

## Variations

**Get canonical spelling.** Use `Normalized` for the address value and keep `Text` and the offsets for display in context. This variant uses the equivalent `SequenceKind.IpAddress` filter.

```csharp
using RecursiveAI.OneNlp;

string text = "📍 192.0.2.1, 2001:0DB8::1.";
foreach (var hit in Nlp.Sequences(text))
    if (hit.Kind == SequenceKind.IpAddress)
        Console.WriteLine(hit.Normalized);
```

```text
192.0.2.1
2001:db8::1
```

## Related recipes

- [Extract dates and numbers](https://recursiveai.org/onenlp/cookbook/extract-dates-and-numbers/)
- [Align regex matches to tokens](https://recursiveai.org/onenlp/cookbook/align-regex-matches-to-tokens/)

## The short answer

Call `Nlp.Sequences(text)` to run OneNLP’s built-in pattern recognizers, which look for shapes such as dotted IPv4 addresses, and keep the `IP_ADDRESS` results. That finds both IPv4 and IPv6 in the same call; no model package is needed. Results are in text order. Use `Text` to show what the author wrote, `Start` and `End` to highlight it in the original C# string, or `Normalized` for canonical address text.

## How can I extract IP addresses from text?

Use `Nlp.Sequences` to find addresses inside free text without writing a regex; use [`IPAddress.TryParse`](https://learn.microsoft.com/en-us/dotnet/api/system.net.ipaddress.tryparse) when you already have a candidate string and want to check whether it parses as an address. The snippet scans the whole string, so the comma between the addresses and the closing period aren’t part of either match. Use the offsets with the exact string you passed in. If you trim or normalize the string afterward, or measure positions in a UTF-8 buffer instead, those offsets may point somewhere else.

The [recognizer’s overlap rules](https://recursiveai.org/onenlp/docs/recognizers/#overlaps) return only non-overlapping matches. A longer URL match can take precedence over the IP address it contains. Filtering for `IP_ADDRESS` won’t return that nested host as a separate hit.

## How do I know if an address is IPv4 or IPv6?

Both families get the same `IP_ADDRESS` label. IPv4 matches have four decimal parts separated by dots, each from 0 to 255. The recognizer rejects leading zeros and longer dotted runs, so it won’t take the first four parts of `1.2.3.4.5` as an address. IPv6 uses colon-separated hexadecimal groups and can shorten a run of zero groups to `::`. An IPv6 literal can have an IPv4 tail, so dots alone don’t tell you which family it belongs to.

[OneNLP’s IP rules](https://recursiveai.org/onenlp/docs/recognizers/#ip-address) don’t read zone identifiers such as `%eth0` and don’t recognize `::` on its own. The canonical IPv6 spelling in `Normalized` uses lowercase letters, removes leading zeros in hex groups, and compresses the longest run of at least two zero groups. Recognition checks the syntax in the text, not whether an address exists or can be reached.
