揭秘Regex 我的lxfRegex現實[源]

來源:互聯網
上載者:User

Regex的用途十分的廣泛,常用在搜尋和替換,幾乎所有的程式設計語言都對其支援各種的地方,如js,java ,c#都有自己的實作類別,所以在使用起來也是十分的方便.
 很想弄明白他在每種語言中是如何做的,於是在沒有弄得源碼前,先動手用C#語言去做了一個支援Regex實現的類,目前實現功能十分的簡單,支援IsMatch和Relace的處理基底字元的實現,
代碼有諸多的不足,希望多多見諒.

範例程式碼:

lxfRegex lxf = new lxfRegex(txtregex.Text);


            StringBuilder sb = new StringBuilder();
            sb.AppendLine("括弧結果");

            foreach (MatchUnit mu in lxf.Matches(txtSrc.Text))
            {
                sb.AppendLine("Match匹配" + ind++);
                int i = 0;
                for (int j = 0; j < mu.Groups.Count; j++)
                {
                    sb.AppendLine("Group" + j + mu.Groups[j].Value);

                    foreach (CaptureUnit t in mu.Groups[j].Captures)
                    {
                        sb.Append(" Capture" + i);
                        sb.Append(" Index:" + t.Index + " Length:" + t.Length);
                        sb.Append(" = " + t.Value);
                        sb.AppendLine();
                        i++;
                    }
                }
            }
            this.txtMatch.Text = sb.ToString();

示範位置:
http://www.iyuyan.com/regex愛語言
下載源碼:

原始碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.