คุณต้องตรวจสอบโปรโตคอลเพื่อยืนยันความถูกต้อง
http https
ด้วยเหตุนี้ คุณจะต้องตรวจสอบ .com, .in, .org เป็นต้น
สำหรับสิ่งนี้ ให้ใช้นิพจน์ทั่วไปต่อไปนี้ −
(http|http(s)?://)?([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)?
ต่อไปนี้เป็นรหัส −
ตัวอย่าง
using System;
using System.Text.RegularExpressions;
namespace RegExApplication {
class Program {
private static void showMatch(string text, string expr) {
Console.WriteLine("The Expression: " + expr);
MatchCollection mc = Regex.Matches(text, expr);
foreach (Match m in mc) {
Console.WriteLine(m);
}
}
static void Main(string[] args) {
string str = "https://example.com";
Console.WriteLine("Matching URL...");
showMatch(str, @"^(http|http(s)?://)?([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)?");
Console.ReadKey();
}
}
} ผลลัพธ์
Matching URL... The Expression: ^(http|http(s)?://)?([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)? https://example.com