C#Regex Match给出了错误的结果

Miv*_*web 0 c# regex

 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;
Run Code Online (Sandbox Code Playgroud)

子域的结果总是:tenant1.example.be而不是仅仅tenant1.

任何人?

ctr*_*ron 7

您只需要匹配的第一组:

var subdomain = match.Success ? match.Groups[1].Value : null;
Run Code Online (Sandbox Code Playgroud)

  • 其实你会想要match.Groups [1] .Value. (5认同)