標籤:
正則超級連結
string pattenhref = "<a([^>])*>"; string pattenhrefend = "</a([^>])*>"; Regex rg = new Regex(pattenhref, RegexOptions.Multiline | RegexOptions.Singleline); MatchCollection m = rg.Matches(strtext); for (int i = 0; i < m.Count; i++) { MessageBox.Show(m[i].Result); }取反 string pattenhref = "<a([^>])*>"; string pattenhrefend = "</a([^>])*>"; Regex rg = new Regex(pattenhref, RegexOptions.Multiline | RegexOptions.Singleline); MatchCollection m = rg.Matches(strtext); for (int i = 0; i < m.Count; i++) { MessageBox.Show(m[i].Result); }引用樓主 firendlys 的回複:現在,我想要做的是,想做一個匹配,匹配除了 true false 這2個單詞之外的任何字元。VB.NET code//如果是單個字元 (?is)(?!true|false). //如果是多個字元(?is)((?!true|false).)+ string pattenhref = "<a([^>])*>"; string pattenhrefend = "</a([^>])*>"; Regex rg = new Regex(pattenhref, RegexOptions.Multiline | RegexOptions.Singleline); strtext = rg.Replace(strtext, ""); MessageBox.Show(strtext);
做採集用到的正則
string regexstr = @"<(?!br|p|/p).*?>"; //去除所有標籤,只剩img,br,phtml = Regex.Replace(html, regexstr, string.Empty, RegexOptions.IgnoreCase);
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Text.RegularExpressions;using System.Collections;using System.IO;namespace kuaifa.Common{ public static class HttpCJ { public static string GetHTML(string url, string encoding) { WebClient web = new WebClient(); byte[] buffer = web.DownloadData(url); return Encoding.GetEncoding(encoding).GetString(buffer); } public static string GetSubValue(string str, string s, string e) { Regex rg = new Regex("(?<=(" + s + "))[.\\s\\S]*?(?=(" + e + "))", RegexOptions.Multiline | RegexOptions.Singleline); return rg.Match(str).Value; } public static ArrayList GetMatchesStr(string htmlCode, string strRegex) { ArrayList al = new ArrayList(); Regex r = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline); MatchCollection m = r.Matches(htmlCode); for (int i = 0; i < m.Count; i++) { bool rep = false; string strNew = m[i].ToString(); // 過濾重複的URL foreach (string str in al) { if (strNew == str) { rep = true; break; } } if (!rep) al.Add(strNew); } al.Sort(); return al; } public static string GetDataFromUrl(string url) { string str = string.Empty; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); //設定Http頭; request.AllowAutoRedirect = true; request.AllowWriteStreamBuffering = true; request.Referer = ""; request.Timeout = 10 * 1000; //request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)"; HttpWebResponse response = null; try { response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { //根據http應答頭來判別編碼 string Characterset = response.CharacterSet; Encoding encode; if (Characterset != "") { if (Characterset == "ISO-8859-1") { Characterset = "gb2312"; } encode = Encoding.GetEncoding(Characterset); } else { encode = Encoding.Default; } //聲明一個記憶體流來貯存http應答流 Stream Receivestream = response.GetResponseStream(); MemoryStream mstream = new MemoryStream(); byte[] bf = new byte[255]; int count = Receivestream.Read(bf, 0, 255); while (count > 0) { mstream.Write(bf, 0, count); count = Receivestream.Read(bf, 0, 255); } Receivestream.Close(); mstream.Seek(0, SeekOrigin.Begin); //從記憶體流裡讀取字串這裡涉及到了編碼方案 StreamReader reader = new StreamReader(mstream, encode); char[] buf = new char[1024]; count = reader.Read(buf, 0, 1024); while (count > 0) { str += new string(buf, 0, 1024); count = reader.Read(buf, 0, 1024); } reader.Close(); mstream.Close(); } } catch (Exception ex) { GetDataFromUrl(url); } finally { if (response != null) response.Close(); } return str; } }}
byte與string與char.txt
byte[] ByteArray={120,121,123,128,129,130}; char[] CharArray=System.Text.Encoding.ASCII.GetChars(ByteArray); for(int i=0;i <CharArray.Length;i++) { //其實此處128後面的編碼無法顯示 Console.WriteLine( "{0}:{1} ",i,CharArray[i]); } Console.WriteLine(); //此處得到我們訂製的ASCII字串,然後類比您的測試 string s=new String(CharArray); Console.WriteLine(s); byte[] newByteArray=System.Text.Encoding.ASCII.GetBytes(s); 讀:TextBox1.Text=File.ReadAllText("d:/b.txt",Encoding.Default);寫:File.WriteAllText("d:/a.txt", TextBox1.Text, Encoding.Default); 追加:File.AppendAllText("d:/a.txt", TextBox1.Text, Encoding.Default); string s = System.Text.Encoding.Default.GetString(ib);byte[] b = System.Text.Encoding.Default.GetBytes(s); MemoryStream buf = new MemoryStream(b); Image image = Image.FromStream(buf, true); pictureBox_File.Image = image;項目中我用的漢字,String args = new String(data,"GB2312"); 好用! Encoding encoding = Encoding.GetEncoding(Encode); byte[] bytes = encoding.GetBytes(PostStr); //string retstr = System.Text.Encoding.UTF8.GetString(bytes); //MessageBox.Show(retstr);public string recvMessage(){ byte[] recvBytes = new byte[Int32.Parse(ss)]; int bytes=0; bytes += s.Receive(recvBytes, recvBytes.Length, 0); byte[] gb = Encoding.Convert(utf8, gb2312, recvBytes); string sGb = gb2312.GetString(gb);}
電腦整理的時候發現的一些