一、将相对url转换成绝对url 方法
//将相对uri转换成绝对uri 实例
Uri uri = new Uri("http://www.gongjuji.net");
Uri uri2 = new Uri(uri, "abc/1234.html");
Console.WriteLine(uri2.ToString());//http://www.gongjuji.net/abc/1234.html
Uri uri3 = new Uri(uri, "/md5/encrypt");
Console.WriteLine(uri3.ToString());//http://www.gongjuji.net/md5/encrypt
二、识别字符串中的链接
相对靠谱:
//相对比较靠谱2,但是匹配不出连接中的中文,如果中文转码是可以的
Regex re = new Regex(@"((https?|ftp|file):\/\/[\-A-Za-z0-9+&@#\/%?=~_|!:,.;]*[\-A-Za-z0-9+&@#\/%=~_|])", RegexOptions.IgnoreCase | RegexOptions.Multiline);
//相对靠谱3,对于url中的,不做间隔
//string pattern = @"(https?://\S+)";
//Regex re = new Regex(pattern);
MatchCollection mc = re.Matches(source);
foreach (Match m in mc)
{
// string url = m.Result("${url}");
string url = m.Value;
if (string.IsNullOrEmpty(url) == false)
{
list.Add(url);
}
}
实例1:
string s = "例如:http://www.asd.com,http://wwww.gongjuji.net?name=zhangsan&age=10,http://md5.gongjuji.net/dencrypt/";
Regex re = new Regex(@"(?<url>http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?)");
MatchCollection mc = re.Matches(s);
foreach (Match m in mc)
{
string url = m.Result("${url}");
Console.WriteLine(url);
}
实例2:
string s = "例如:http://www.asd.com";
Regex re = new Regex(@"(?<url>http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?)");
MatchCollection mc = re.Matches(s);
foreach (Match m in mc)
{
s = s.Replace(m.Result("${url}"), String.Format("<a href='{0}'>{0}</a>", m.Result("${url}")));
}
Console.WriteLine(s);
四、判断字符串是否是URL
/// <summary>
/// 验证是否是URL链接
/// </summary>
/// <param name="str">指定字符串</param>
/// <returns></returns>
public static bool IsURL(string str)
{
string pattern = @"^(https?|ftp|file|ws)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$";
return IsMatch(pattern, str);
}
/// <summary>
/// 判断一个字符串,是否匹配指定的表达式(区分大小写的情况下)
/// </summary>
/// <param name="expression">正则表达式</param>
/// <param name="str">要匹配的字符串</param>
/// <returns></returns>
public static bool IsMatch(string expression, string str)
{
Regex reg = new Regex(expression);
if (string.IsNullOrEmpty(str))
return false;
return reg.IsMatch(str);
}
三、url中添加或修改参数
public static void Test4()
{
//生成random
Random rand = new Random(Environment.TickCount);
int num = rand.Next(1, 100);
/*
* http://news.sina.com.cn
* http://news.sina.com.cn?page=1
* http://news.sina.com.cn?page=1&ts=1341 &&
* http://news.sina.com.cn?ts=14321&page=1 ?&
* http://news.sina.com.cn?u=2&ts=14321&page=1 &&
*/
string url = "http://news.sina.com.cn?u=2&ts=14321&page=1";
string ParamText = "ts";
string ParamValue = num.ToString();
Console.WriteLine(BuildUrl(url, ParamText, ParamValue));
Console.Read();
}
//url里有key的值,就替换为value,没有的话就追加.
public static string BuildUrl(string url, string ParamText, string ParamValue)
{
Regex reg = new Regex(string.Format("{0}=[^&]*", ParamText), RegexOptions.IgnoreCase);
Regex reg1 = new Regex("[&]{2,}", RegexOptions.IgnoreCase);
string _url = reg.Replace(url, "");
if (_url.IndexOf("?") == -1)
_url += string.Format("?{0}={1}", ParamText, ParamValue);//?
else
_url += string.Format("&{0}={1}", ParamText, ParamValue);//&
_url = reg1.Replace(_url, "&");
_url = _url.Replace("?&", "?");
return _url;
}