常用的regex
http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
regex(@"http[s]?://([/\w-]+\.)+[\w-]+(/[/\w- ./?%&=]*)?");
http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
//##################提取url字符串
System.Text.RegularExpressions.Regex regex=new Regex(@"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
MatchCollection mc=regex.Matches(instr);
if(mc.Count==1)
{
ststr=mc[0].Value;
}
if(mc.Count>1)
{
ststr=mc[1].Value;
}
//##################提取: http://******.aspx 以^ http.//开头 中间符意字符 .+
以.aspx结尾
System.Text.RegularExpressions.Regex regext=new Regex(@"^http.//.+.aspx$");
MatchCollection mct=regext.Matches(ststr);
if(mct.Count>0)
{
temp=mct[0].Value;
}
//############################同理提取http://*********.html 和http://********.htm
System.Text.RegularExpressions.Regex regexe=new Regex(@"^http.//.+.htm$");
MatchCollection mce=regexe.Matches(ststr);
if(mce.Count>0)
{
temp=mce[0].Value;
}
System.Text.RegularExpressions.Regex regexd=new Regex(@"^http.//.+.html$");
MatchCollection mcd=regexd.Matches(ststr);
if(mcd.Count>0)
{
temp=mcd[0].Value;
}
/////
private void btn_count_Click(object sender, System.EventArgs e)
{
string httpdate=GetHttpData(this.txt_url.Text);
//<(?!tr|td)[^>]*>
//<[^>]+>
// strOutput = Replace(strOutput, "<", "<")
//strOutput = Replace(strOutput, ">", ">")
Regex r = new Regex(@"<(?!tr|td)[^>]*>");
string ddd=r.Replace(httpdate,"").Replace("\r\n","").Replace("\t","");
string dd1=ddd.Replace("<","").Replace(">","").Replace("\"","").Trim();
Regex re=new Regex(@"^toptd.+webtrends$");
Match mc=re.Match(dd1);
string estr=mc.Value;
}
public string GetHttpData(string Url)
{
string sException=null;
string sRslt=null;
WebResponse oWebRps=null;
WebRequest oWebRqst=WebRequest.Create(Url);
oWebRqst.Timeout=50000;
try
{
oWebRps=oWebRqst.GetResponse();
}
catch(WebException e)
{
sException=e.Message.ToString();
Response.Write(sException);
}
catch(Exception e)
{
sException=e.ToString();
Response.Write(sException);
}
finally
{
if(oWebRps!=null)
{
StreamReader oStreamRd=new StreamReader(oWebRps.GetResponseStream());
sRslt=oStreamRd.ReadToEnd();
oStreamRd.Close();
oWebRps.Close();
}
}
return sRslt;
}
///private void btn_count_Click(object sender, System.EventArgs e)
{
string httpdate=GetHttpData(this.txt_url.Text);
//<(?!tr|td)[^>]*>
//<[^>]+>
// strOutput = Replace(strOutput, "<", "<")
//strOutput = Replace(strOutput, ">", ">")
// Regex r = new Regex(@"<(?!tr|td)[^>]*>");
// string ddd=r.Replace(httpdate,"").Replace("\r\n","").Replace("\t","");
// string dd1=ddd.Replace("<","").Replace(">","").Replace("\"","").Trim();
//
// Regex re=new Regex(@"^toptd.+webtrends$");
// Match mc=re.Match(dd1);
// string estr=mc.Value;
//
string output=httpdate.Replace("\"","").Replace("\r\n","").Replace("\t","");
Regex r=new Regex(@"<tr valign=top>.+</tr>");
Match m=r.Match(output);
string estr=m.Value;
// MatchCollection mc=r.Matches(estr);
//
// int mc1=mc.Count;
// string af=mc[0].Value;
}
public string GetHttpData(string Url)
{
string sException=null;
string sRslt=null;
WebResponse oWebRps=null;
WebRequest oWebRqst=WebRequest.Create(Url);
oWebRqst.Timeout=50000;
try
{
oWebRps=oWebRqst.GetResponse();
}
catch(WebException e)
{
sException=e.Message.ToString();
Response.Write(sException);
}
catch(Exception e)
{
sException=e.ToString();
Response.Write(sException);
}
finally
{
if(oWebRps!=null)
{
StreamReader oStreamRd=new StreamReader(oWebRps.GetResponseStream());
sRslt=oStreamRd.ReadToEnd();
oStreamRd.Close();
oWebRps.Close();
}
}
return sRslt;
}
///////////
private void btn_count_Click(object sender, System.EventArgs e)
{
string httpdate=GetHttpData(this.txt_url.Text);
string output=httpdate.Replace("\"","").Replace("\r\n","").Replace("\t","");
Regex r=new Regex(@"<tr valign=top>.+</tr>");
/////////
Match m=r.Match(output);
string estr=m.Value;
Regex nr=new Regex(@"<tr.+?>",RegexOptions.IgnoreCase);
string newstr=nr.Replace(estr,"<tr>");
Regex rr=new Regex(@"<a.+?>",RegexOptions.IgnoreCase);
string nn=rr.Replace(newstr,"").Replace("</a>","");
Regex rrr=new Regex(@"<td.+?>",RegexOptions.IgnoreCase);
string nnn=rrr.Replace(nn,"<td>");
//string nstr=nnn.Replace("<","").Replace(">","");
string nnstr=nnn.Replace("<tr>","str").Replace("</tr>","etr").Replace("<td>","ftd").Replace("</td>","ltd");
//
ArrayList arr=new ArrayList();
string [] arrstring=new string[100];
string [] arrtdstring=new string[6];
int startindex=0;
int endindex=0;
for(int i=0;i<nnstr.Length-2;i++)
{
string tempstr=nnstr.Substring(i,3);
if(tempstr.StartsWith("str"))
{
startindex=i+3;
}
if(tempstr.StartsWith("etr"))
{
endindex=i-1;
int len=endindex-startindex+1;
string tempstring=nnstr.Substring(startindex,len);
arr.Add(nnstr.Substring(startindex,len));
}
if(arr.Count==3)
{
break;
}
}
arr.CopyTo(arrstring ,0);
System.Text.StringBuilder sb =new StringBuilder();
//
// for(int i=0;i<arrstring.Length;i++)
// {
//
// sb.Append(arrstring[i]+"\n\r");
// }
// Response.Write(sb.ToString());
//
//列表项
// for(int j=0;j<3;j++)
// {
string tdstr=arrstring[0];
ArrayList arrtd=new ArrayList();
for(int i=0;i<tdstr.Length-2;i++)
{
string tempstr=tdstr.Substring(i,3);
if(tempstr.StartsWith("ftd"))
{
startindex=i+3;
}
if(tempstr.StartsWith("ltd"))
{
endindex=i-1;
int len=endindex-startindex+1;
string tempstring=tdstr.Substring(startindex,len);
arrtd.Add(tdstr.Substring(startindex,len));
}
if(arrtd.Count==6)
{
break;
}
}
arrtd.CopyTo(arrtdstring,0);
//单项
for(int k=0;k<arrtdstring.Length;k++)
{
sb.Append(arrtdstring[k]+"<br>");
}
// }
Response.Write(sb.ToString());
//
}
public string GetHttpData(string Url)
{
string sException=null;
string sRslt=null;
WebResponse oWebRps=null;
WebRequest oWebRqst=WebRequest.Create(Url);
oWebRqst.Timeout=50000;
try
{
oWebRps=oWebRqst.GetResponse();
}
catch(WebException e)
{
sException=e.Message.ToString();
Response.Write(sException);
}
catch(Exception e)
{
sException=e.ToString();
Response.Write(sException);
}
finally
{
if(oWebRps!=null)
{
StreamReader oStreamRd=new StreamReader(oWebRps.GetResponseStream());
sRslt=oStreamRd.ReadToEnd();
oStreamRd.Close();
oWebRps.Close();
}
}
return sRslt;
}
regex(@"http[s]?://([/\w-]+\.)+[\w-]+(/[/\w- ./?%&=]*)?");
http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
//##################提取url字符串
System.Text.RegularExpressions.Regex regex=new Regex(@"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
MatchCollection mc=regex.Matches(instr);
if(mc.Count==1)
{
ststr=mc[0].Value;
}
if(mc.Count>1)
{
ststr=mc[1].Value;
}
//##################提取: http://******.aspx 以^ http.//开头 中间符意字符 .+
以.aspx结尾
System.Text.RegularExpressions.Regex regext=new Regex(@"^http.//.+.aspx$");
MatchCollection mct=regext.Matches(ststr);
if(mct.Count>0)
{
temp=mct[0].Value;
}
//############################同理提取http://*********.html 和http://********.htm
System.Text.RegularExpressions.Regex regexe=new Regex(@"^http.//.+.htm$");
MatchCollection mce=regexe.Matches(ststr);
if(mce.Count>0)
{
temp=mce[0].Value;
}
System.Text.RegularExpressions.Regex regexd=new Regex(@"^http.//.+.html$");
MatchCollection mcd=regexd.Matches(ststr);
if(mcd.Count>0)
{
temp=mcd[0].Value;
}
/////
private void btn_count_Click(object sender, System.EventArgs e)
{
string httpdate=GetHttpData(this.txt_url.Text);
//<(?!tr|td)[^>]*>
//<[^>]+>
// strOutput = Replace(strOutput, "<", "<")
//strOutput = Replace(strOutput, ">", ">")
Regex r = new Regex(@"<(?!tr|td)[^>]*>");
string ddd=r.Replace(httpdate,"").Replace("\r\n","").Replace("\t","");
string dd1=ddd.Replace("<","").Replace(">","").Replace("\"","").Trim();
Regex re=new Regex(@"^toptd.+webtrends$");
Match mc=re.Match(dd1);
string estr=mc.Value;
}
public string GetHttpData(string Url)
{
string sException=null;
string sRslt=null;
WebResponse oWebRps=null;
WebRequest oWebRqst=WebRequest.Create(Url);
oWebRqst.Timeout=50000;
try
{
oWebRps=oWebRqst.GetResponse();
}
catch(WebException e)
{
sException=e.Message.ToString();
Response.Write(sException);
}
catch(Exception e)
{
sException=e.ToString();
Response.Write(sException);
}
finally
{
if(oWebRps!=null)
{
StreamReader oStreamRd=new StreamReader(oWebRps.GetResponseStream());
sRslt=oStreamRd.ReadToEnd();
oStreamRd.Close();
oWebRps.Close();
}
}
return sRslt;
}
///private void btn_count_Click(object sender, System.EventArgs e)
{
string httpdate=GetHttpData(this.txt_url.Text);
//<(?!tr|td)[^>]*>
//<[^>]+>
// strOutput = Replace(strOutput, "<", "<")
//strOutput = Replace(strOutput, ">", ">")
// Regex r = new Regex(@"<(?!tr|td)[^>]*>");
// string ddd=r.Replace(httpdate,"").Replace("\r\n","").Replace("\t","");
// string dd1=ddd.Replace("<","").Replace(">","").Replace("\"","").Trim();
//
// Regex re=new Regex(@"^toptd.+webtrends$");
// Match mc=re.Match(dd1);
// string estr=mc.Value;
//
string output=httpdate.Replace("\"","").Replace("\r\n","").Replace("\t","");
Regex r=new Regex(@"<tr valign=top>.+</tr>");
Match m=r.Match(output);
string estr=m.Value;
// MatchCollection mc=r.Matches(estr);
//
// int mc1=mc.Count;
// string af=mc[0].Value;
}
public string GetHttpData(string Url)
{
string sException=null;
string sRslt=null;
WebResponse oWebRps=null;
WebRequest oWebRqst=WebRequest.Create(Url);
oWebRqst.Timeout=50000;
try
{
oWebRps=oWebRqst.GetResponse();
}
catch(WebException e)
{
sException=e.Message.ToString();
Response.Write(sException);
}
catch(Exception e)
{
sException=e.ToString();
Response.Write(sException);
}
finally
{
if(oWebRps!=null)
{
StreamReader oStreamRd=new StreamReader(oWebRps.GetResponseStream());
sRslt=oStreamRd.ReadToEnd();
oStreamRd.Close();
oWebRps.Close();
}
}
return sRslt;
}
///////////
private void btn_count_Click(object sender, System.EventArgs e)
{
string httpdate=GetHttpData(this.txt_url.Text);
string output=httpdate.Replace("\"","").Replace("\r\n","").Replace("\t","");
Regex r=new Regex(@"<tr valign=top>.+</tr>");
/////////
Match m=r.Match(output);
string estr=m.Value;
Regex nr=new Regex(@"<tr.+?>",RegexOptions.IgnoreCase);
string newstr=nr.Replace(estr,"<tr>");
Regex rr=new Regex(@"<a.+?>",RegexOptions.IgnoreCase);
string nn=rr.Replace(newstr,"").Replace("</a>","");
Regex rrr=new Regex(@"<td.+?>",RegexOptions.IgnoreCase);
string nnn=rrr.Replace(nn,"<td>");
//string nstr=nnn.Replace("<","").Replace(">","");
string nnstr=nnn.Replace("<tr>","str").Replace("</tr>","etr").Replace("<td>","ftd").Replace("</td>","ltd");
//
ArrayList arr=new ArrayList();
string [] arrstring=new string[100];
string [] arrtdstring=new string[6];
int startindex=0;
int endindex=0;
for(int i=0;i<nnstr.Length-2;i++)
{
string tempstr=nnstr.Substring(i,3);
if(tempstr.StartsWith("str"))
{
startindex=i+3;
}
if(tempstr.StartsWith("etr"))
{
endindex=i-1;
int len=endindex-startindex+1;
string tempstring=nnstr.Substring(startindex,len);
arr.Add(nnstr.Substring(startindex,len));
}
if(arr.Count==3)
{
break;
}
}
arr.CopyTo(arrstring ,0);
System.Text.StringBuilder sb =new StringBuilder();
//
// for(int i=0;i<arrstring.Length;i++)
// {
//
// sb.Append(arrstring[i]+"\n\r");
// }
// Response.Write(sb.ToString());
//
//列表项
// for(int j=0;j<3;j++)
// {
string tdstr=arrstring[0];
ArrayList arrtd=new ArrayList();
for(int i=0;i<tdstr.Length-2;i++)
{
string tempstr=tdstr.Substring(i,3);
if(tempstr.StartsWith("ftd"))
{
startindex=i+3;
}
if(tempstr.StartsWith("ltd"))
{
endindex=i-1;
int len=endindex-startindex+1;
string tempstring=tdstr.Substring(startindex,len);
arrtd.Add(tdstr.Substring(startindex,len));
}
if(arrtd.Count==6)
{
break;
}
}
arrtd.CopyTo(arrtdstring,0);
//单项
for(int k=0;k<arrtdstring.Length;k++)
{
sb.Append(arrtdstring[k]+"<br>");
}
// }
Response.Write(sb.ToString());
//
}
public string GetHttpData(string Url)
{
string sException=null;
string sRslt=null;
WebResponse oWebRps=null;
WebRequest oWebRqst=WebRequest.Create(Url);
oWebRqst.Timeout=50000;
try
{
oWebRps=oWebRqst.GetResponse();
}
catch(WebException e)
{
sException=e.Message.ToString();
Response.Write(sException);
}
catch(Exception e)
{
sException=e.ToString();
Response.Write(sException);
}
finally
{
if(oWebRps!=null)
{
StreamReader oStreamRd=new StreamReader(oWebRps.GetResponseStream());
sRslt=oStreamRd.ReadToEnd();
oStreamRd.Close();
oWebRps.Close();
}
}
return sRslt;
}