[英]Regex help - domain is not validating
我在驗證域時遇到問題。 我究竟做錯了什么?
Regex re = new Regex("^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\\.[a-zA-Z]{2,}$");
if (re.IsMatch(domain.Text))
// if (Regex.IsMatch(domain.Text, @"^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\\.[a-zA-Z]{2,}$"))
warningLabel.Text = "Domain format is invalid!"; // domain: " + domain.Text;
我使用正則表達式檢查器進行了檢查,並得到“您的模式匹配,但其中沒有(捕獲(組))與主題字符串中的任何內容匹配”。
為什么我沒有收到無效字符的錯誤?
謝謝。
您的Regex基本上是正確的(但請參見下面的鏈接),並且此測試方法可以證實這一點:
public static void Test()
{
Regex re = new Regex("^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\\.[a-zA-Z]{2,}$");
var m=re.Match("test.com");
Console.WriteLine(m.Groups[0].Value);
m= re.Match("more-messy-domain-0234-example.xx");
Console.WriteLine(m.Groups[0].Value);
}
輸出:
test.com
更雜亂,domain0234-example.xx
請注意,此處對域名的正則表達式進行了很好的討論: 使用RegEx進行域名驗證
正則表達式中不包含一些細微的情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.