電腦整理的時候發現的一些

來源:互聯網
上載者:User

標籤:

正則超級連結

                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);}

 

電腦整理的時候發現的一些

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.