如何在C#中使用正则表达式验证URL?
要进行验证,您需要检查协议。
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
热门推荐
10 好朋友夫妻祝福语简短
11 简短的出行工作祝福语
12 嫂子生日的祝福语简短
13 国庆求婚祝福语简短精辟
14 年前幸福祝福语大全简短
15 国庆中秋祝福语简短搞笑
16 端午发给客户祝福语简短
17 相恋人回去祝福语简短
18 生日牌祝福语英语简短