Recursive AI

Extract a social media handle from a URL 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.

Read X (Twitter) or Instagram handles without opening the link.

Terminal
dotnet add package RecursiveAI.OneContact

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneContact;
var url = WebAddress.Parse(
    "https://x.com/Example_Name?ref=card");
if (url.Flags.HasFlag(WebFacts.SocialX))
    Console.WriteLine(url.Path.Trim('/'));

Unzip the example, then dotnet run.

Text
Example_Name

This recipe extracts X (Twitter) and Instagram handles only. OneContact recognizes other profile shapes, such as YouTube channel URLs, but the example doesn’t extract handles from them.

Full program

C#
using RecursiveAI.OneContact;
 
string[] links =
[
    "https://x.com/Example_Name?ref=card",
    "https://www.instagram.com/Example.Name/",
    "https://x.com/home",
    "https://www.instagram.com/p/example/",
];
 
foreach (string link in links)
{
    if (!WebAddress.TryParse(link, out var url))
        Console.WriteLine("Invalid URL");
    else if ((url.Flags & (WebFacts.SocialX | WebFacts.SocialInstagram)) != 0)
        Console.WriteLine($"Handle: {url.Path.Trim('/')}");
    else
        Console.WriteLine("No recognized X or Instagram profile");
}

dotnet run prints:

Text
Handle: Example_Name
Handle: Example.Name
No recognized X or Instagram profile
No recognized X or Instagram profile

A URL that won’t parse is reported separately from a valid URL without either profile flag. A flag describes the link’s shape, not whether the account exists.

Variations

If you only need to tell which profile shape matched, read the flags without extracting the name:

C#
using RecursiveAI.OneContact;
var url = WebAddress.Parse(
    "https://instagram.com/Example.Name/");
Console.WriteLine(
    url.Flags.HasFlag(WebFacts.SocialInstagram));
Text
True

The short answer

To extract a handle from an X or Instagram profile URL in C#, parse it with WebAddress, check WebFacts.SocialX or WebFacts.SocialInstagram, then trim the slashes from Path. For those two flags, OneContact found one nonempty handle segment. It supplies the flag and the parsed path, not a separate handle property. The example returns Example_Name without looking up the account.

Read the parsed path instead of splitting the original URL on /. Path is separate from the query string, so ?ref=card never becomes part of the handle. It also preserves the path’s case. In the second example, the trailing slash comes off with Trim('/'), and the period in Example.Name stays.

Check the profile flag before trimming. The profile rules check the host and the path for HTTP and HTTPS URLs. For X, including twitter.com links, the pattern accepts one segment of up to 15 letters, digits or underscores, with exclusions for known site pages such as home. Instagram’s single segment can also contain periods, with a maximum of 30 characters. Its /p/ post links are not profile matches.

Do all social profile URLs contain a handle?

No. OneContact also recognizes shapes such as YouTube’s /channel/<id> and LinkedIn’s /company/<name>. The first is a channel ID and the second is a company slug; neither should pass through the single-segment extraction above. If you accept another network, check its own flag and handle only the path forms you want to support. Keep the platform alongside any extracted handle when using it as a lookup field.

The recognition is local. It doesn’t follow redirects, check whether an account exists, or tell you whether someone controls it. Leave the handle blank when the URL doesn’t match a shape your application handles; a nonempty path alone is not a username.

All onecontact recipes