xor*_*wer 29 c# regex visual-studio
我写了正则表达式验证URL,可能就像
example.com
www.example.com
所有4个都在工作
现在,如果我输入的文本(www.example.com - thisisincorrect),它允许我输入并保存到db
我使用的正则表达式是:
http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
Run Code Online (Sandbox Code Playgroud)
和
([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
Run Code Online (Sandbox Code Playgroud)
请帮忙!
Ale*_*lex 70
您不需要URL的正则表达式,请使用System.Uri类.例如,通过使用Uri.IsWellFormedUriString方法:
bool isUri = Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute);
Run Code Online (Sandbox Code Playgroud)
Gar*_*een 30
添加^和$
^http(s)?://([\w-]+.)+[\w-]+(/[\w- ./?%&=])?$
Run Code Online (Sandbox Code Playgroud)
这匹配string(^)和end($)的开头
小智 16
最佳正则表达式:
private bool IsUrlValid(string url)
{
string pattern = @"^(http|https|ftp|)\://|[a-zA-Z0-9\-\.]+\.[a-zA-Z](:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$";
Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
return reg.IsMatch(url);
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*cas 11
试试看:
bool IsValidURL(string URL)
{
string Pattern = @"^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$";
Regex Rgx = new Regex(Pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
return Rgx.IsMatch(URL);
}
Run Code Online (Sandbox Code Playgroud)
它将接受这样的 URL:
| 归档时间: |
|
| 查看次数: |
70213 次 |
| 最近记录: |