简体   繁体   中英

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?

您只需要匹配的第一组:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM