dotnet add package RecursiveAI.OneNlpRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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}");
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, 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.
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);
192.0.2.1 2001:db8::1
Related recipes
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 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 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 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.