Mivaweb
Mivaweb

Reputation: 5712

C# Regex Match gives wrong result

 var hostName = "tenant1.example.be";

 var match = Regex.Match(hostName, @"([A-Za-z0-9]+)\.example\.be$", RegexOptions.IgnoreCase);
 var subdomain = match.Success ? match.Value : null;

Result for subdomain is always: tenant1.example.be instead of just tenant1.

Anyone?

Upvotes: 0

Views: 713

Answers (1)

ctron
ctron

Reputation: 694

You need only the first group of the match:

var subdomain = match.Success ? match.Groups[1].Value : null;

Upvotes: 7

Related Questions