C# C/s结构例如我访问:http://www.abc.com/temp/bbs/index.asp 则希望获得:index.asp
例如我访问:http://adfadf.com 则没有任何后缀获得。
例如我访问:http://www.mmkkk.com/aaa/ 则没有任何后缀获得。
例如我访问:http://WWW.7799.net/ 也是没有任何后缀获得。
例如我访问:http://www.xx.net/aaa/bbb/default.htm 则希望获得:default.htm
例如我访问:http://www.javaeye.com/topic/17434540002 则希望获得:17434540002
例如我访问:http://www.ccty.org/login.do?a=54&pwline=http://ccc.net/3ffa.php 则希望获得:login.do?a=54&pwline=http://ccc.net/3ffa.php
例如我访问:http://www.21cn.com/index 则希望获得:index 这个正则该如何写呢?谢谢~
例如我访问:http://adfadf.com 则没有任何后缀获得。
例如我访问:http://www.mmkkk.com/aaa/ 则没有任何后缀获得。
例如我访问:http://WWW.7799.net/ 也是没有任何后缀获得。
例如我访问:http://www.xx.net/aaa/bbb/default.htm 则希望获得:default.htm
例如我访问:http://www.javaeye.com/topic/17434540002 则希望获得:17434540002
例如我访问:http://www.ccty.org/login.do?a=54&pwline=http://ccc.net/3ffa.php 则希望获得:login.do?a=54&pwline=http://ccc.net/3ffa.php
例如我访问:http://www.21cn.com/index 则希望获得:index 这个正则该如何写呢?谢谢~
这个比较麻烦,要判断 ? ,可能还有 & 。
{
String strReturn = String.Empty;
Regex objRegex = new Regex(@"^http://(\w+\.){1,}\w+(/\w+)*/(?<S>.*)$",RegexOptions.IgnoreCase); MatchCollection objCollection = objRegex.Matches(strURL);
if (objCollection.Count > 0)
{
strReturn = objCollection[0].Groups["S"].Value;
}
return strReturn;
}
String[] strCollections = new String[]{
"http://www.abc.com/temp/bbs/index.asp",
"http://adfadf.com",
"http://www.mmkkk.com/aaa/",
"http://WWW.7799.net/",
"http://www.xx.net/aaa/bbb/default.htm",
"http://www.javaeye.com/topic/17434540002",
"http://www.ccty.org/login.do?a=54&pwline=http://ccc.net/3ffa.php",
"http://www.21cn.com/index"
}; foreach (String objItem in strCollections)
{
String strItem = GetString(objItem);
if (String.IsNullOrEmpty(strItem))
strItem = objItem; Response.Write(strItem + "<br/>");
}
//结果
//index.asp
//http://adfadf.com
//http://www.mmkkk.com/aaa/
//http://WWW.7799.net/
//default.htm
//17434540002
//login.do?a=54&pwline=http://ccc.net/3ffa.php
//index
"^https?://[^\?\&]+/(?<v>.*)$"
//Result
//http://www.abc.com/temp/bbs/index.asp Groups["v"] : index.asp
//http://adfadf.com NoMatch
//http://www.mmkkk.com/aaa/ Groups["v"] : ""
//http://WWW.7799.net/ Groups["v"] : ""
//http://www.xx.net/aaa/bbb/default.htm Groups["v"] : default.htm
//http://www.javaeye.com/topic/17434540002 Groups["v"] : 17434540002
//http://www.ccty.org/login.do?a=54&pwline=http://ccc.net/3ffa.php
// Groups["v"] : login.do?a=54&pwline=http://ccc.net/3ffa.php
//http://www.21cn.com/index Groups["v"] : index