Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正则表达式匹配给出错误的结果_C#_Regex - Fatal编程技术网

C# 正则表达式匹配给出错误的结果

C# 正则表达式匹配给出错误的结果,c#,regex,C#,Regex,子域的结果总是:tenant1.example.be,而不仅仅是tenant1 有人吗?您只需要比赛的第一组: 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; 正则表达式工作

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


有人吗?

您只需要比赛的第一组:

 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;

正则表达式工作得很好。您提取的是整个匹配值,而不是捕获的组。您确定这不是条件运算符或赋值运算符中的错误吗?为什么使用a-Za-z和RegexOptions.IgnoreCase?您可以使用var subdomain=match.Groups[1]。value??字符串。空;实际上,您需要match.Groups[1].Value。当然!该死,使用这个正则表达式已经很久了。谢谢,伙计
var subdomain = match.Success ? match.Groups[1].Value : null;